Candies
Time Limit: 1500MS | Memory Limit: 131072K | |
Total Submissions: 38621 | Accepted: 10891 |
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 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 2
1 2 5
2 1 4
Sample Output
5
Hint
32-bit signed integer type is capable of doing all arithmetic.
Source
POJ Monthly--2006.12.31, Sempr
问题链接:POJ3159 Candies
问题描述:n个人,m个信息,每行的信息是3个数字,A,B,C,表示B比A多出来的糖果不超过C个,问你,n号人最多比1号人多几个糖果。
解题思路:差分约束+Dijkstra+堆优化+前向星
AC的C++程序:
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
const int N=30005;
const int INF=0x3f3f3f3f;
int dist[N];
bool vis[N];
//前向星实现图的邻接表
int M;
int head[N];
struct Edge{
int to,next,w;
}g[160000];
void add(int from,int to,int w)
{
g[M].next=head[from],g[M].to=to,g[M].w=w;
head[from]=M++;
}
struct Node{
int u,w;
Node(){}
Node(int u,int w):u(u),w(w){}
bool operator<(const Node &a)const
{
return w>a.w;
}
};
void dijkstra(int s)//s表示起点
{
memset(dist,INF,sizeof(dist));
memset(vis,false,sizeof(vis));
priority_queue<Node>q;
q.push(Node(s,0));
dist[s]=0;
while(!q.empty()){
Node f=q.top();
q.pop();
int u=f.u;
if(!vis[u]){
vis[u]=true;
for(int i=head[u];i!=-1;i=g[i].next){
int v=g[i].to;
if(!vis[v]){
if(dist[v]>dist[u]+g[i].w){
dist[v]=dist[u]+g[i].w;
q.push(Node(v,dist[v]));
}
}
}
}
}
}
int main()
{
int n,m,a,b,c;
scanf("%d%d",&n,&m);
memset(head,-1,sizeof(head));
M=0;
while(m--){
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
}
dijkstra(1);//计算1号站到其他站的最短路径
printf("%d\n",dist[n]);
return 0;
}