POJ3159(最短路)
Time Limit: 1500MS | Memory Limit: 131072K | |
Total Submissions: 27051 | Accepted: 7454 |
Description
During the kindergarten days, flymouse was 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 of candies they got with others. A kid A could had the idea that though it might be the case that another kid B was better than him in some aspect and therefore had a reason for deserving more candies than he did, he should never get a certain number of candies fewer than B did 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 his 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 was 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 two integers N and M not exceeding 30 000 and 150 000 respectively. 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, B and c in order, meaning that kid A believed that kid B should never get overc 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 2 1 2 5 2 1 4
Sample Output
5
题意:给出N个孩子,再给出M个限制。A,B,c表示孩子B的糖果最多比孩子A的糖果多c个。问N号孩子最多比1号孩子多多少的糖果。
思路:转化为求1号结点到N号结点的最短路问题。
/* dijkstra Accepted 3112KB 547ms G++ */ #include"cstdio" #include"cstring" #include"queue" using namespace std; const int MAXN=150005; const int INF=0x3fffffff; struct Edge{ int to,cost,next; }es[MAXN]; struct P{ int fi,se; P(int cfi,int cse):fi(cfi),se(cse){} bool operator<(const P& a) const { return fi > a.fi; } }; int heap[MAXN]; int V,E; void add_edge(int u,int v,int co) { es[E].to=v; es[E].cost=co; es[E].next=heap[u]; heap[u]=E; E++; } int d[MAXN]; int dijkstra(int s) { for(int i=1;i<=V;i++) d[i]=INF; priority_queue<P> que; que.push(P(0,s)); d[s]=0; while(!que.empty()) { P p=que.top();que.pop(); int v=p.se; if(d[v]<p.fi) continue; for(int i=heap[v];i!=-1;i=es[i].next) { Edge e=es[i]; if(d[e.to]>d[v]+e.cost) { d[e.to]=d[v]+e.cost; que.push(P(d[e.to],e.to)); } } } return d[V]; } int main() { int N,M; while(scanf("%d%d",&N,&M)!=EOF) { memset(heap,-1,sizeof(heap)); V=N,E=0; for(int i=0;i<M;i++) { int u,v,co; scanf("%d%d%d",&u,&v,&co); add_edge(u,v,co); } int ans=dijkstra(1); printf("%d\n",ans); } return 0; }
下面是用栈实现的spfa算法。用队列实现会TLE。
/* spfa Accepted 3112KB 547ms G++ */ #include"cstdio" #include"cstring" using namespace std; const int MAXN=150005; const int INF=0x3fffffff; struct Edge{ int to,cost,next; }es[MAXN]; int stack[MAXN],top; int head[MAXN]; int V,E; void add_edge(int u,int v,int co) { es[E].to=v; es[E].cost=co; es[E].next=head[u]; head[u]=E; E++; } int d[MAXN]; int vis[MAXN]; int spfa(int s) { for(int i=1;i<=V;i++) d[i]=INF; memset(vis,0,sizeof(vis)); top=0; stack[top++]=s; vis[1]=s,d[s]=0; while(top!=0) { int v=stack[--top]; vis[v]=0; for(int i=head[v];i!=-1;i=es[i].next) { Edge e=es[i]; if(d[e.to]>d[v]+e.cost) { d[e.to]=d[v]+e.cost; if(!vis[e.to]) { vis[e.to]=1; stack[top++]=e.to; } } } } return d[V]; } int main() { int N,M; while(scanf("%d%d",&N,&M)!=EOF) { memset(head,-1,sizeof(head)); V=N,E=0; for(int i=0;i<M;i++) { int u,v,co; scanf("%d%d%d",&u,&v,&co); add_edge(u,v,co); } int ans=spfa(1); printf("%d\n",ans); } return 0; }