差分约束POJ3159Candies(spfa+stack)解题报告

来源:互联网 发布:ps软件什么好 编辑:程序博客网 时间:2024/06/04 01:11

Candies
Time Limit: 1500MS Memory Limit: 131072K
Total Submissions: 26267 Accepted: 7197
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
差分约束题,转化成求最短路径,求点1到点InKidNum的距离。

// 2520K    547MS#include<algorithm>#include<cstring>#include<iostream>#include<stack>#include<cstdio>#define MAXEDGE 150005#define MAXVERTEX 30005#define inf 1000000000using namespace std;int InKidNum,InEdgeNum;int AdjEdge[MAXEDGE];int vis[MAXVERTEX];int dis[MAXVERTEX];//点1到每个点的最短距离struct Edge{    int endV;    int w;    int PriEdge;}edge[MAXEDGE];void init(){    memset(vis,0,sizeof(vis));    memset(AdjEdge,0,sizeof(AdjEdge));//当遍历到起始点的邻边为0时,结束遍历    fill(dis+1,dis+InKidNum+1,inf);    dis[1]=0;//源点到源点距离为0}void addEdge(int startV,int endV,int w,int i){    edge[i].endV=endV;    edge[i].w=w;    edge[i].PriEdge=AdjEdge[startV];    AdjEdge[startV]=i;}void spfa(){    stack<int> s;    s.push(1);//源点入栈    while(!s.empty()){        int frontV=s.top();s.pop();vis[frontV]=0;//栈首出栈        for(int i=AdjEdge[frontV];i!=0;i=edge[i].PriEdge){            if(dis[edge[i].endV]> dis[frontV]+edge[i].w){                dis[edge[i].endV]= dis[frontV]+edge[i].w;                if(!vis[edge[i].endV]){//入栈                    vis[edge[i].endV]=1;                    s.push(edge[i].endV);                }            }        }    }    printf("%d\n",dis[InKidNum]);}int main(){    freopen("input.txt","r",stdin);    freopen("output.txt","w",stdout);    while(scanf("%d%d",&InKidNum,&InEdgeNum)!=EOF){        int tmp1,tmp2,tmp3;        init();        for(int i=1;i<=InEdgeNum;i++){            scanf("%d%d%d",&tmp1,&tmp2,&tmp3);            addEdge(tmp1,tmp2,tmp3,i);//加入第i条边,边从1开始编号        }        spfa();    }    return 0;}
0 0
原创粉丝点击