POJ3159 Candies【差分約束+最短路】

Enjoy_process發表於2018-10-19

Candies

Time Limit: 1500MS   Memory Limit: 131072K
Total Submissions: 38621   Accepted: 10891

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 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

問題連結:POJ3159 Candies

問題描述:n個人,m個資訊,每行的資訊是3個數字,A,B,C,表示B比A多出來的糖果不超過C個,問你,n號人最多比1號人多幾個糖果。

解題思路:差分約束+Dijkstra+堆優化+前向星

AC的C++程式:

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

using namespace std;

const int N=30005;
const int INF=0x3f3f3f3f;
int dist[N];
bool vis[N];
//前向星實現圖的鄰接表
int M; 
int head[N]; 
struct Edge{
	int to,next,w;
}g[160000];

void add(int from,int to,int w)
{
	g[M].next=head[from],g[M].to=to,g[M].w=w;
	head[from]=M++;
} 

struct Node{
	int u,w;
	Node(){}
	Node(int u,int w):u(u),w(w){}
	bool operator<(const Node &a)const
	{
		return w>a.w;
	} 
};

void dijkstra(int s)//s表示起點
{
	memset(dist,INF,sizeof(dist));
	memset(vis,false,sizeof(vis));
	priority_queue<Node>q;
	q.push(Node(s,0));
	dist[s]=0;
	while(!q.empty()){
		Node f=q.top();
		q.pop();
		int u=f.u;
		if(!vis[u]){
			vis[u]=true;
			for(int i=head[u];i!=-1;i=g[i].next){
				int v=g[i].to;
				if(!vis[v]){
					if(dist[v]>dist[u]+g[i].w){
						dist[v]=dist[u]+g[i].w;
						q.push(Node(v,dist[v]));
					}
				}
			}
		}
	}
}

int main()
{
	int n,m,a,b,c;
	scanf("%d%d",&n,&m);
	memset(head,-1,sizeof(head));
	M=0;
	while(m--){
		scanf("%d%d%d",&a,&b,&c);
		add(a,b,c); 
	}
	dijkstra(1);//計算1號站到其他站的最短路徑
	printf("%d\n",dist[n]);
	return 0;
}
 

 

相關文章