poj 3159 Cadies(優先列dijkstra+ヒープ)

3493 ワード

テーマリンク
http://poj.org/problem?id=3159
キャンディーズ
Time Limit: 1500 MS
 
メモリLimit: 131313722 K
Total Submissions: 26118
 
Acceepted: 7147
Description
During the kindergarten days、fliymouse was the momomoitor of his class.Occaasionally the head- teache broughtthe kid s of ffflymouse s s s s clasa largbag of candies and and had fflaymouse distibute them.All thekids lods lods lods lodddcandinininininininininininininininininininininininind candscscscscscaaaaaaaaaaaaaaffffs thethethethethethethethethethethethethethethethethethethethethethethethetheriririririmmmmmmmmmmmbe the case that another kid B wasbetter than him m m in some aspect and therefore had a reason for deserving moe candies candininininininininindid、heshuld never get a certain number of candies fefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefeaaaaaaaaaaaaaattttttd d d d d d d d d d d d d d d d d d d d d d d d d mamaterhohohomememememememememememememememememememememe..。
snoopy shred class with flaymouse at time.fymouse always compred thenumbed the number of his candies with th th th th th of snoopy's.He wanted to makethe difference between thenumbebebers the numbebebersasasasasasasaslargaaaafffffrererererererererererererererererererererererererererereres s s s s s s s s s aaaaaaaaaaafffffffffrerererererererererererererererererererewhat was the larget difference he could make out of it?
Input
The input contains a single test cases.The test cases starts with a line with two integers N and M not exceeding 30 000 and 150 respively. N is the number of kids in the class and the kids were numbeed 1 through N.snoopy and flymouse were always numberd 1 and N.The-nフォロワー M lineach holding three integers A, B and c in order、meaning that kid A believed that kid B ショルド・never get overc candies more than he did.
Output
Output one line with only the larget st difference desired.The difference is garanted to be finite.
Sample Input
2 2
1 2 5
2 1 4
Sample Output
5
タイトルの大意:
N人の子供(N<=3000)がお菓子を分けます。Mの関係があります。各関係形: A B.   B番目の学生はA番目の学生よりも多く分けられたキャンディの数を表しています。Cを超えてはいけません。  第N人の学生を求めて、最大で第1人の学生より多くいくつかのキャンディーを分けることができます。 
ACコード:
#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct Node{
    int k;
    int w;
};
bool operator<(const Node &a,const Node &b)
{return a.w>b.w;}
priority_queue<Node> pq;
bool bUsed[30010]={0};
vector<vector<Node> > v;
const unsigned int inf=1000000000;
int main()
{
    int n,m,a,b,c;
    int i,j,k;
    Node p;
    scanf("%d%d",&n,&m);
    v.clear();
    v.resize(n+1);
    memset(bUsed,0,sizeof(bUsed));
    for(i=1;i<=m;i++)
    {
        scanf("%d%d%d",&a,&b,&c);
        p.k=b;
        p.w=c;
        v[a].push_back(p);
    }
    p.k=1;
    p.w=0;
    pq.push(p);
    while(!pq.empty()){
        p=pq.top();
        pq.pop();
        if(bUsed[p.k])
            continue;
        bUsed[p.k]=true;
        if(p.k==n)
            break;
        for(i=0,j=v[p.k].size();i<j;i++){
            Node q;
            q.k=v[p.k][i].k;
            if(bUsed[q.k]) continue;
            q.w=p.w+v[p.k][i].w;
            pq.push(q);
        }
    }
    printf("%d",p.w);
    return 0;
}