码迷,mamicode.com
首页 > 其他好文 > 详细

poj3159--Candies(差分约束)

时间:2014-08-22 22:36:24      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:des   os   使用   io   strong   for   数据   ar   art   

Candies
Time Limit: 1500MS   Memory Limit: 131072K
Total Submissions: 23131   Accepted: 6224

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

一个差分约束的模板题,明确的给出了 a b w  a比b最多多w,所以 b  - a <= w 。 那么由b到a建立有向边,权值为w,跑一次由1到n最短路,得出最多的差值

坑点就是使用queue会超时,只能stack,可能是数据的问题

 

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <stack>
#define inf 0x3f3f3f3f
using namespace std;
struct node
{
    int v,w,next;
} edge[150010];
int head[30001],dis[30001],vis[30001],cnt,n,m,s;
void add(int u,int v,int w)
{
    edge[cnt].v=v;
    edge[cnt].w=w;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
void spfa()
{
    for(int i=1;i<=n;i++)
    {
        dis[i]=inf;
        vis[i]=0;
    }
    dis[s]=0;
    vis[s]=1;
    stack<int >Q;
    Q.push(s);
    while(!Q.empty())
    {
        int u=Q.top();
        Q.pop();
        vis[u]=0;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v;
            if(dis[v]>dis[u]+edge[i].w)
            {
                dis[v]=dis[u]+edge[i].w;
                if(!vis[v])
                {
                    vis[v]=1;
                    Q.push(v);
                }
            }
        }
    }
}
int main()
{
    int i,j,u,v,w;
    while(~scanf("%d%d",&n,&m))
    {
        s=1;
        for(i=1;i<=n;i++)
        head[i]=-1;
        cnt=0;
        for(i=0; i<m; i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,w);
        }
        spfa();
        printf("%d\n",dis[n]);
    }
    return 0;
}


 

poj3159--Candies(差分约束)

标签:des   os   使用   io   strong   for   数据   ar   art   

原文地址:http://blog.csdn.net/winddreams/article/details/38762299

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!