Topic Link: Http://poj.org/problem?
id=3159
Candies
| Time Limit: 1500MS |
|
Memory Limit: 131072K |
| Total Submissions: 22516 |
|
Accepted: 6047 |
Description During The Kindergarten days, Flymouse is the monitor of his class. Occasionally the head-teacher brought the kids of Flymouse ' s Class A large bag of candies and had flymouse distribute them . All the kids loved candies very much and often compared the numbers for candies they got with others. A kid A could had the idea that though it might is the case that another kid B is better than him in some aspect and ther Efore had a reason for deserving more candies than he did, he should never get a certain number of candies fewer than B di D no matter how many candies he actually got, otherwise he would feel dissatisfied and go to the head-teacher to complain About Flymouse ' s biased distribution. Snoopy shared class with Flymouse at that time. Flymouse always compared the number of he candies with that of Snoopy ' s. He wanted to make the difference between the numbers as large as possible while keeping every kid satisfied. Now he had just got another bag of candies from the Head-teacher, what is the largest difference he could make out of it? Input The input contains a single test cases. The test cases starts with a line with both integers N and M not exceeding and respectivel Y. N is the number of kids in the class and the kids were numbered 1 throughN. Snoopy and Flymouse were always numbered 1 and N. Then follow M lines each holding three integers A, B and C in order, meaning that kid A believed that kid B should never get over C candies more than he did. Output Output one line with only the largest difference desired. The difference is guaranteed to be finite. Sample Input 2 21 2 52 1 4
Sample Output 5
Hint 32-bit signed integer type is capable of doing all arithmetic.Source POJ monthly--2006.12.31, SEMPR |
The shortest path of the classic problem, with the heap optimization is finished. It's a little review.
#include <iostream> #include <algorithm> #include <cmath> #include <cstdio> #include < cstring> #include <queue> #include <stack> #include <string> #include <vector> #include < set> #include <map> #include <bitset> #include <cstdlib> #define CLR (a) memset (A,0,sizeof (a)) using namespace std;struct node{int k,w;}; BOOL operator < (const node &a,const node &b) {return A.W>B.W;} priority_queue<node> q;vector<vector<node> > G;bool used[30010]={0};int Main () {int n,m; Node p; while (~SCANF ("%d%d", &n,&m)) {g.clear (); G.resize (n+1); CLR (used); for (int i=1;i<=m;i++) {int a,b,c; scanf ("%d%d%d", &a,&b,&c); P.k=b;p.w=c; G[a].push_back (P); } p.k=1;p.w=0; Q.push (P); while (!q.empty ()) {p=q.top (); Q.pop (); if (USED[P.K]) continue; Used[p.k]=true; if (P.K==n) break; Node tmp; for (int i=0;i<g[p.k].size (); i++) {tmp.k=g[p.k][i].k; if (USED[TMP.K]) continue; TMP.W=P.W+G[P.K][I].W; Q.push (TMP); }} printf ("%d\n", P.W); } return 0;}
POJ 3159 Candies