【POJ3159】【差分系统】【dij+priority_queue】
2015-08-24 10:07
591 查看
Candies
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
Sample Output
Hint
32-bit signed integer type is capable of doing all arithmetic.
Source
POJ Monthly--2006.12.31, Sempr
Time Limit: 1500MS | Memory Limit: 131072K | |
Total Submissions: 25849 | Accepted: 7059 |
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 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
#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_back int 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 ; } 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; }
相关文章推荐
- android-如何关闭AlertDialog.Builder对话框
- poj2031-Building a Space Station(最小生成树,kruskal,prime)
- TLD(Tracking-Learning-Detection)学习与源码理解之(build)
- Building a Space Station 1718 (三维的 最小生成树)
- Building a Space Station 2031 (三维的 最小生成树 prim)
- 第2组UI-ImageView及子类
- Web开发工具之HBuilder初探
- 使用requirejs实现模块化编程
- [LeetCode] Missing Number (A New Questions Added Today)
- UIScrollView_UIPageControl
- Android开发之Volley定制自己的Request
- css3的媒体查询(Media Queries)
- Java中SerialVersionUID的作用
- UIScrollView
- Windows GUI 开发历史
- Windows GUI 开发历史
- POJ2778 DNA Sequence AC自动机+快速幂+DP
- Android UI设计:ImageView
- [CareerCup] 6.4 Blue Eyes People on Island 岛上的蓝眼人
- [LeetCode#115]Distinct Subsequences