您的位置:首页 > 其它

POJ 1201 Candies [差分约束系统]

2016-07-31 19:19 281 查看
Candies

Time Limit: 1500MS Memory Limit: 131072KB 64bit IO Format: %lld & %llu

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.

此题大意就是 x[j] - x[i] <= c[i,j]

由于spfa中有 if(dis[v] > dis[u] + c[i,j]) dis[v] = dis[u] + c[i,j] , 那么可以得到 dis[v] <= dis[u] + c[i,j]

所以此题可以看成从 i –> j 连了一条 c 的边

然后spfa求最短路即可。。

但是数据要求用stack。。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<string>
#include<iomanip>
#include<ctime>
#include<climits>
#include<cctype>
#include<algorithm>
using namespace std;
const int maxn=30005;
const int maxm=150005;
struct Edge
{
int to,next;
int val;
}edge[maxm];
int head[maxn];
int maxedge;
inline void addedge(int u,int v,int c)
{
edge[++maxedge]=(Edge){v,head[u],c};
head[u]=maxedge;
}
int n,m;
inline void init()
{
scanf("%d%d",&n,&m);
memset(head,-1,sizeof(head));
maxedge=-1;
for(int i=1;i<=m;i++)
{
int u,v,c;
scanf("%d%d%d",&u,&v,&c);
addedge(u,v,c);
}
}
int dis[maxn];
bool inque[maxn];
/*inline int spfa(int S,int T)
{
deque <int> que;
memset(dis,0x3f,sizeof(dis));
dis[S]=0;inque[S]=true;
que.push_back(S);
int sum=0,tot=1;
while(!que.empty())
{
int u=que.front();que.pop_front();
if(dis[u]*tot>sum) { que.push_back(u);continue; }
else inque[u]=false,tot--,sum-=dis[u];
for(int i=head[u];~i;i=edge[i].next)
{
int v=edge[i].to;
if(dis[v]>dis[u]+edge[i].val)
{
dis[v]=dis[u]+edge[i].val;
if(inque[v]) continue;
inque[v]=true;tot++,sum+=dis[v];
if(!que.empty())
if(dis[v]<dis[que.front()]) que.push_front(v);
else que.push_back(v);
else que.push_back(v);
}
}
}
return dis[T];
}*/
inline int spfa(int S,int T)
{
stack <int> que;
memset(dis,0x3f,sizeof(dis));
dis[S]=0;inque[S]=true;
que.push(S);
while(!que.empty())
{
int u=que.top();que.pop();inque[u]=false;
for(int i=head[u];~i;i=edge[i].next)
{
int v=edge[i].to;
if(dis[v]>dis[u]+edge[i].val)
{
dis[v]=dis[u]+edge[i].val;
if(!inque[v]) que.push(v),inque[v]=true;
}
}
}
return dis[T];
}
int main()
{
freopen("candy.in","r",stdin);
freopen("candy.out","w",stdout);
init();
cout<<spfa(1,n)<<endl;
return 0;
}


Summary :

x[j] - x[i] <= c[i,j] 求最短路

x[j] - x[i] >= c[i,j] 求最长路。。

不要被题目所求最大值最小值所迷惑。。。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息