1. 程式人生 > >poj3159—Candies(差分規劃+dijsktra)

poj3159—Candies(差分規劃+dijsktra)

Candies

Time Limit: 1500MS Memory Limit: 131072K
Total Submissions: 38441 Accepted: 10826

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 3159

差分約束+SPFA */

/* /* 給n個人派糖果,給出m組資料,每組資料包含A,B,c  三個數, 意思是A的糖果數比B少的個數不多於c,即B的糖果數 - A的糖果數<= c 。 最後求n 比 1 最多多多少糖果。 【解題思路】 這是一題典型的差分約束題。不妨將糖果數當作距離,把相差的最大糖果數看成有向邊AB的權值, 我們得到 dis[B]-dis[A]<=w(A,B)。看到這裡,我們聯想到求最短路時的鬆弛技術, 即if(dis[B]>dis[A]+w(A,B), dis[B]=dis[A]+w(A,B)。 即是滿足題中的條件dis[B]-dis[A]<=w(A,B),由於要使dis[B] 最大, 所以這題可以轉化為最短路來求。 這題如果用SPFA 演算法的話,則需要注意不能用spfa+queue 來求,會TLE ,而是用 spfa + stack

或者用鄰接矩陣版的dijskstra演算法 */

程式碼:

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
/*
 * 使用優先佇列優化Dijkstra演算法
 * 複雜度O(ElogE)
 * 注意對vector<Edge>E[MAXN]進行初始化後加邊
 */
const long long INF=1e18;
const int maxm=5300100;
const int maxn=200000+100;
typedef long long LL;
struct qnode
{
    int v;
    int c;
    qnode(int _v=0,int _c=0):v(_v),c(_c){}
    bool operator <(const qnode &r)const
    {
        return c>r.c;
    }
};
struct Edge
{
    int v,cost;
    int next;
};
Edge edge[maxm];
int tol;
int head[maxm];
bool vis[maxn];
LL dist[maxn];
void Dijkstra(int n,int start)//點的編號從1開始
{
    memset(vis,false,sizeof(vis));
    for(int i=1;i<=n;i++)dist[i]=INF;
    priority_queue<qnode>que;
    while(!que.empty())que.pop();
    dist[start]=0;
    que.push(qnode(start,0));
    qnode tmp;
    while(!que.empty())
    {
        tmp=que.top();
        que.pop();
        int u=tmp.v;
        if(vis[u])continue;
        vis[u]=true;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v;
            int cost=edge[i].cost;
            if(!vis[v]&&dist[v]>dist[u]+cost)
            {
                dist[v]=dist[u]+cost;
                que.push(qnode(v,dist[v]));
            }
        }
    }
}
void add(int u,int v,int w)
{
    edge[tol].v=v;
    edge[tol].cost=w;
    edge[tol].next=head[u];
    head[u]=tol++;
}
void init(){
    memset(head,-1,sizeof(head));
    tol=0;
}
LL a[maxn],c[maxn];
LL shuchu[maxn];
int main()
{
    int cas=0;
    int t,tt,n,m,s,x;
    int aa,bb,cc;

        scanf("%d%d",&n,&m);
        init();
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&aa,&bb,&cc);
            add(aa,bb,cc);
        }
        Dijkstra(n,1);
        printf("%d\n",dist[n]);
    return 0;
}