poj3159 Candies--单源最短路径&差分约束

来源:互联网 发布:java api接口开发 编辑:程序博客网 时间:2024/05/22 04:26

原题链接: http://poj.org/problem?id=3159


一:原题内容

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 integersN 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 throughN. snoopy and flymouse were always numbered 1 and N. Then followM lines each holding three integers A, B and c in order, meaning that kidA 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 21 2 52 1 4

Sample Output

5

二:分析理解

班上有n个同学,现在有一些糖要分给他们,设第i个同学得到的糖为p[i],分糖必须满足条件:第i个同学要求第j个同学的糖不能超过自己k个,即p[j] - p[i] <= k,k >= 0。要求在满足这些条件的情况下,求出p[n] - p[1]的最大值。

由p[j] - p[i] <= k可得p[j] <= p[i] + k

在单源最短路径的算法中有一步是“若mindis[j] > mindis[i] + dis[i][j],则mindis[j] = mindis[i] + dis[i][j],这样就满足mindis[j] <= mindis[i] + dis[i][j]”。因此本题可以使用单源最短路径的算法来解决,对于“第i个同学要求第j个同学的糖不能超过自己k个,即p[j] - p[i] <= k,k >= 0”这个条件,建立一条边(i->j)=k,由于不含负权路径,因此建立完所有边之后以第1个同学为起点,采用Dijkstra+Heap求最短路径即可。除了Dijkstra也可以利用Spfa+Stack算法求解,但由于数据原因必须用Stack,如果用Queue则会超时。


三:AC代码

#include<iostream>  #include<string.h>#include<algorithm>using namespace std;struct Edge{int v;//箭头指向的位置,在此不存储u节点int dis;//边的距离,也就是权值int next;//持有同一个u的下一个边};Edge edge[150005];int head[30005];int dis[30005];int sta[30005];bool visited[30005];int n, m;int a, b, c;int num;void AddEdge(int u, int v, int w){edge[num].dis = w;edge[num].v = v;edge[num].next = head[u];head[u] = num++;}void Spfa(int s){int top = 0;fill(dis, dis + 30005, INT_MAX);sta[++top] = s;visited[s] = 1;dis[s] = 0;while (top){int u = sta[top--];visited[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].dis){dis[v] = dis[u] + edge[i].dis;if (!visited[v]){sta[++top] = v;visited[v] = 1;}}}}}int main(){scanf("%d%d", &n, &m);num = 0;memset(head, -1, sizeof(head));while (m--){scanf("%d%d%d", &a, &b, &c);AddEdge(a, b, c);}Spfa(1);printf("%d\n", dis[n]);return 0;}


1 0
原创粉丝点击