POJ3159 Candies(Dijkstra+差分约束)

POJ3159 Candies(Dijkstra+差分约束)

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

题意

给n个小朋友分糖吃,有m个小朋友觉得自己不能比某一个小朋友少分到w颗糖。这道题是使用了差分约束的最短路,利用Dijkstra求最短路即可求解。(差分约束类的题目让你算最大值就求出最短路,最小值就求出最长路)

#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
#include<functional> 
#include<map>
//#include<unordered_map>
#define lowbit(x) ((x)&-(x));
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e6+10,NN=1e4+10,INF=0x3f3f3f3f,LEN=110;
const ll MOD=1e9+7;
const ull seed=31;
struct Edge{
	int next,to,dis;
}edge[N];
struct Node{
	int id,dis;
	Node(int id,int dis):id(id),dis(dis){}
	bool friend operator<(const Node &a,const Node &b){
		return a.dis>b.dis;
	}
};
int num_edge,n,m;
int head[N],dis[N];
bool done[N];
void add_edge(int from,int to,int dis){
	edge[++num_edge].next=head[from];
	edge[num_edge].to=to;
	edge[num_edge].dis=dis;
	head[from]=num_edge;
} 
void dijkstra(){
	int s=1;
	for(int i=1;i<=n;i++){
		dis[i]=INF;
		done[i]=false;
	}
	dis[s]=0;
	priority_queue<Node>q;
	q.push(Node(s,dis[s]));
	while(!q.empty()){
		Node u=q.top();
		q.pop();
		if(done[u.id]) continue;
		done[u.id]=true;
		for(int i=head[u.id];i!=-1;i=edge[i].next){
			int v=edge[i].to,w=edge[i].dis;
			if(done[v]) continue;
			if(dis[v]>u.dis+w){
				dis[v]=u.dis+w;
				q.push(Node(v,dis[v]));
			}
		}
	}
	printf("%d\n",dis[n]);
}
void init(){
	memset(head,-1,sizeof head);
	num_edge=0;
}
int main(){
	init();
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++){
		int u,v,w;
		scanf("%d%d%d",&u,&v,&w);
		add_edge(u,v,w);
	}
	dijkstra();
}

猜你喜欢

转载自blog.csdn.net/Hc_Soap/article/details/107721877