【POJ3159】【差分系统】【dij+priority_queue】
来源:互联网 发布:多宝视弱视训练软件 编辑:程序博客网 时间:2024/05/22 17:37
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 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
Hint
Source
#include <iostream>#include <cstring>#include <cmath>#include <queue>#include <stack>#include <list>#include <map>#include <set>#include <string>#include <cstdlib>#include <cstdio>#include <algorithm>#include <assert.h>using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++)#define per(i,a,n) for (int i=n-1;i>=a;i--)#define mp push_backint n,m;struct Qnode{int idx,dis;Qnode(int _idx,int _dis):idx(_idx),dis(_dis) {}; bool operator< (const Qnode&x) const{return dis > x.dis;}};int d[30100];bool vis[30100];int H[30100];int top;struct Edge{int v,c,next;}edge[150010];void addedge(int u,int v,int c){edge[top].v = v;edge[top].c = c;edge[top].next = H[u];H[u] = top++;}int dij(int st){for(int i=1;i<=n;i++){vis[i] = false;d[i] = 0x3f3f3f3f;}d[st] = 0;priority_queue<Qnode> pq;pq.push(Qnode(st,0));for(int i=0;i<n-1;i++){while(!pq.empty() && vis[pq.top().idx]) pq.pop();assert(!pq.empty());Qnode cur = pq.top();vis[cur.idx] = true;pq.pop();for(int i=H[cur.idx];i!=-1;i=edge[i].next){Edge e = edge[i];if(!vis[e.v] && d[e.v] > d[cur.idx] + e.c){d[e.v] = d[cur.idx] + e.c;pq.push(Qnode(e.v,d[e.v]));}}}return d[n];}int main(){ while(scanf("%d%d",&n,&m) != EOF){top = 0;memset(H,-1,sizeof(H));rep(i,0,m){int u,v,c;scanf("%d%d%d",&u,&v,&c);addedge(u,v,c);}printf("%d\n",dij(1));} return 0;}
- 【POJ3159】【差分系统】【dij+priority_queue】
- dij(priority_queue)
- poj1201差分系统
- poj3159(差分约束)
- POJ3159 差分约束系统
- poj3159--Candies(差分约束)
- Candies-POJ3159差分约束
- 差分约束 poj3159 Candies
- UVA 11478 Halum 最小差分系统
- 【差分约束系统】Candies POJ3159
- POJ3159差分约束(SPFA)
- poj3159 Candies--单源最短路径&差分约束
- poj3159 Candies(差分约束入门)
- poj3159-Candies差分约束-spfa+栈
- 【POJ3159】Candies-差分约束系统
- poj3159 Candies 最短路&差分约束
- POJ3159 Candies(差分约束)
- 【差分约束系统】POJ3159[Candies]题解
- 黑马程序员----Java中的IO流学习笔记2
- 对抗赛 解题报告
- 自定义EditText实现未输入文字动画提醒效果
- 横论机器学习算法
- linux 创建NFS服务
- 【POJ3159】【差分系统】【dij+priority_queue】
- codeforces 401D D. Roman and Numbers(状态压缩dp+数论)
- android-如何关闭AlertDialog.Builder对话框
- HTML5内嵌视频格式说明
- Unity3D游戏开发之详解 Animation类和Animator类
- Servlet中的过滤器Filter详解
- Linux安装redis
- document.write和innerHTML的区别
- Struts2使用!动态方法调用无效问题