Candies
| Time Limit: 1500MS |
|
Memory Limit: 131072K |
| Total Submissions: 25776 |
|
Accepted: 7033 |
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 through N. Snoopy and Flymouse were always numbered 1 and N. Then follow M lines each holding three integers A, Band C in order, meaning that kid C9>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
uses the queue to time out, using the array to simulate the stack
Test instructions: Everyone will get candy, ask two children The candy gap should be as small as possible, the input data a B c indicates that B children's candy is more than a more than a C, Ask N children up to the first child up to the maximum number of
problems: by test instructions b-a<=c; meet this formula, require to find the minimum gap from the starting point to the end point, can be converted to the shortest path problem
differential constraints! Point me
#include <stdio.h> #include <string.h> #define MAX 160000#define INF 0x3f3f3f#include<queue>using namespace Std;int head[max];int n,m,ans;int dis[max],vis[max];int stack[max];struct node{int u,v,w;int Next;} edge[max];void Add (int u,int v,int W) {Edge[ans].u=u;edge[ans].v=v;edge[ans].w=w;edge[ans].next=head[u];head[u]=ans ++;} void Init () {Ans=0;memset (head,-1,sizeof (Head));} void Getmap () {int i,j;int a,b,c;for (i=1;i<=m;i++) {scanf ("%d%d%d", &a,&b,&c); Add (A,b,c);}} void SPFA (int sx) {int I,j;int topp=0;queue<int>q;memset (vis,0,sizeof (VIS)); for (i=1;i<=n;i++) Dis[i]=inf; Vis[sx]=1;dis[sx]=0;stack[topp++]=sx;while (topp!=0) {int u=stack[topp-1]; Topp--;vis[u]=0;for (i=head[u];i!=-1;i=edge[i].next) {int top=edge[i].v;if (DIS[TOP]>DIS[U]+EDGE[I].W) {Dis[top] =dis[u]+edge[i].w;if (!vis[top]) {vis[top]=1;stack[topp++]=top;}}}} printf ("%d\n", Dis[n]);} int main () {while (scanf ("%d%d", &n,&m)!=eof) {init (); Getmap (); SPFA (1);} return 0;}
Poj 3159 Candies