POJ(3159):Candies(最短路)

Time Limit: 1500MS   Memory Limit: 131072K
Total Submissions: 37837   Accepted: 10648

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 Mlines each holding three integers AB 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

题目大意:

n个人,m个信息,每行的信息是3个数字,A,B,C,表示B比A多出来的糖果不超过C个,问你,n号人最多比1号人多几个糖果。

解题思路:

Dijkstra算法(不适用于有负权边的情况):

如(1-2用Dijkstra算法得到的结果为3)

基本思想:

解决无负权边的带权有向图或无向图的单源最短路问题贪心思想,若离源点s前k-1近的点已经被确定,构成点集P那么从s到离s第k近的点t的最短路径,{s,p 1 ,p 2 …p i ,t}满足s,p 1 ,p 2 …p i ∈P。
 否则假设pi∉P,则因为边权非负,pi到t的路径≥0,则d[pi]≤d[t],pi才是第k近。将pi看作t,重复上面过程,最终一定会有找不pi的情况
 d[i]=min(d[p i ]+cost(p i ,i)),i∉P,p i ∈Pd[t]=min(d[i]) ,i∉P

初始令d[s]=0,d[i]=+∞,P=∅ 找到点i∉P,且d[i]最小把i添入P,对于任意j∉P,若d[i]+cost(i,j)<d[j],则更新d[j]=d[i]+cost(i,j)。

#include<cstdio>
#include<iostream>
#include<vector>
#include<cstring>
#include<queue>

using namespace std;

struct CNode{
    int k;
    int w;
    bool operator<(const CNode&C)const{
        return w > C.w;
    }
    CNode(int kk = 0,int ww = 0):k(kk),w(ww) {}
};
priority_queue<CNode> pq;
bool bUsed[30010];
vector<vector<CNode> >v;
const int inf = 1<<30;

int main(){
    int N,M,a,b,c;
    CNode p;
    scanf("%d%d",&N,&M);
    v.clear();
    v.resize(N+1);
    memset(bUsed,0,sizeof(bUsed));
    for(int i = 1;i <= M;i++){
        scanf("%d%d%d",&a,&b,&c);
        p.k = b;
        p.w = c;
        v[a].push_back(p);
    }
    pq.push(CNode(1,0));
    while(!pq.empty()){
        p = pq.top();
        pq.pop();
        if(bUsed[p.k]) //已求出最短路 
            continue;
        bUsed[p.k] = true;
        if(p.k == N)  //求1-n的距离,所以p.k等于N break;
            break;
        for(int i = 0,j = v[p.k].size();i <j;i++){ //找所有的点到顶点1的距离(例:1—2—3。3-1的权值=3-2的权值 + 2-1的权值)
            CNode q;
            q.k = v[p.k][i].k;
            if(bUsed[q.k])
                continue;
            q.w = p.w+v[p.k][i].w;  
            pq.push(q);
        }
    }
    printf("%d",p.w);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42018521/article/details/82109315