poj 3159 Candies (差分约束 先队列优化Dijkstra 链式前向星存储)

来源:互联网 发布:java随机生成汉字 编辑:程序博客网 时间:2024/05/16 08:19

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.

大致题意:
有多组数据,每组数据首先输入n,m
n表示有n个同学,m表示m组关系
接下来m行
u,v,w,表示同学u要求同学v的糖果数不能多于他超过w个
问同学n和同学1的他糖果数最多相差几个。

思路:差分约束,先队列优化Dijkstra求最短路

差分约束相关知识
http://www.cppblog.com/menjitianya/archive/2015/11/19/212292.html

代码如下

/*采用链式前向星存储边*/#include<iostream>#include<algorithm>#include<string>#include<cstdio>#include<cstring>#include<queue>#include<vector>#include<cstring>using namespace std;const int INF=0x3f3f3f3f;const int MAXN=30005;struct node{    int num;    int dis;    node(int _num=0,int _dis=0):num(_num),dis(_dis){}    friend bool operator <(node a,node b)    {        if(a.dis==b.dis) return a.num>b.num;        return a.dis>b.dis;    }};struct Edge{    int to,cost;// edge[i].to表示编号为i的边所连接下个点 ,edge[i].cost表示编号为i的边 的权值     int next;//edge[i].next表示与编号为i的边同起点的上一条边的编号}edge[200000];int tol;int head[MAXN];//head[i]存储当前以i点为起点的边的编号 int dis[MAXN];int n;priority_queue<node> que;void Dijkstra(int s){    bool vis[MAXN];    memset(vis,0,sizeof(vis));    for(int i=1;i<=n;i++)    dis[i]=INF;    while(!que.empty()) que.pop();    dis[s]=0;//  node u;//  u.num=s;//  u.dis=0;    que.push(node(s,0));    while(!que.empty())    {        node u=que.top();        que.pop();        int num=u.num;        if(vis[num]) continue;        vis[num]=1;        for(int i=head[num];i!=-1;i=edge[i].next)//i表示以num为起点的边的编号         {            int to=edge[i].to;            if(!vis[to]&&dis[to]>dis[num]+edge[i].cost)            {                dis[to]=dis[num]+edge[i].cost;//              node x;//              x.num=to;//              x.dis=dis[to];                que.push(node(to,dis[to]));            }        }    }}void addedge(int u,int v,int w){    edge[tol].to=v;    edge[tol].cost=w;    edge[tol].next=head[u];    head[u]=tol++;} int main() {    int m;    while(scanf("%d%d",&n,&m)!=EOF)    {        tol=0;        memset(head,-1,sizeof(head));        int a,b,c;        while(m--)        {            scanf("%d%d%d",&a,&b,&c);            addedge(a,b,c);          }        Dijkstra(1);        printf("%d\n",dis[n]);      }   return 0;}
0 0
原创粉丝点击