poj 3159 Candies
2016-07-27 22:10
337 查看
poj 3159 Candies
Time Limit: 1500MS Memory Limit: 131072KTotal Submissions: 28461 Accepted: 7868
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 21 2 5
2 1 4
Sample Output
5Hint
32-bit signed integer type is capable of doing all arithmetic.单源最短路径——Dijkstra算法
const int MAX_VERTEX = 30000 + 5; const int INF = 1 << 30; // N个顶点,M条边 int N, M; struct Vertex { // 顶点编号 int v; // 到该顶点的权值 int w; Vertex(int vv, int ww): v(vv), w(ww) {}; bool operator < (const Vertex& ve) const { return w > ve.w; } }; // 邻接表 vector<vector<Vertex> > G; // 访问数组 int visited[MAX_VERTEX]; // 从起始点到某一顶点的距离 int dist[MAX_VERTEX]; // dijkstra算法,求单源最短路径 // 贪心思想,每次都选择当前最优 // 不能有负边,如果需要输出路径 // 可以设置prev数组,在更新dist时更新prev // 邻接表,顶点数,起始顶点 int dijkstra(const vector<vector<Vertex> >& G, int N, int start) { // 初始化 for(int i = 1; i <= N; i++) { dist[i] = INF; visited[i] = 0; } dist[start] = 0; // 优先队列 priority_queue<Vertex> pq; pq.push(Vertex(start, 0)); while(!pq.empty()) { Vertex u = pq.top(); pq.pop(); // 该节点已被访问 if(visited[u.v]) { continue; } // 更新数据 visited[u.v] = 1; for(int i = 0; i < G[u.v].size(); i++) { Vertex v = G[u.v][i]; if(visited[v.v]) { continue; } // 更新距离 if(dist[u.v] + v.w < dist[v.v]) { dist[v.v] = dist[u.v] + v.w; pq.push(v); } } } return dist ; }
源代码
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
const int MAX_VERTEX = 30000 + 5; const int INF = 1 << 30; // N个顶点,M条边 int N, M; struct Vertex { // 顶点编号 int v; // 到该顶点的权值 int w; Vertex(int vv, int ww): v(vv), w(ww) {}; bool operator < (const Vertex& ve) const { return w > ve.w; } }; // 邻接表 vector<vector<Vertex> > G; // 访问数组 int visited[MAX_VERTEX]; // 从起始点到某一顶点的距离 int dist[MAX_VERTEX]; // dijkstra算法,求单源最短路径 // 贪心思想,每次都选择当前最优 // 不能有负边,如果需要输出路径 // 可以设置prev数组,在更新dist时更新prev // 邻接表,顶点数,起始顶点 int dijkstra(const vector<vector<Vertex> >& G, int N, int start) { // 初始化 for(int i = 1; i <= N; i++) { dist[i] = INF; visited[i] = 0; } dist[start] = 0; // 优先队列 priority_queue<Vertex> pq; pq.push(Vertex(start, 0)); while(!pq.empty()) { Vertex u = pq.top(); pq.pop(); // 该节点已被访问 if(visited[u.v]) { continue; } // 更新数据 visited[u.v] = 1; for(int i = 0; i < G[u.v].size(); i++) { Vertex v = G[u.v][i]; if(visited[v.v]) { continue; } // 更新距离 if(dist[u.v] + v.w < dist[v.v]) { dist[v.v] = dist[u.v] + v.w; pq.push(v); } } } return dist ; }
int main() {
scanf("%d%d", &N, &M);
G.clear();
G.resize(N + 1);
for(int i = 0; i < M; i++) {
int A, B, C;
scanf("%d%d%d", &A, &B, &C);
G[A].push_back(Vertex(B, C));
}
printf("%d\n", dijkstra(G, N, 1));
return 0;
}
相关文章推荐
- 初学ACM - 组合数学基础题目PKU 1833
- POJ ACM 1001
- POJ ACM 1002
- 1611:The Suspects
- POJ1089 区间合并
- POJ 2159 Ancient Cipher
- POJ 2635 The Embarrassed Cryptographe
- POJ 3292 Semi-prime H-numbers
- POJ 2773 HAPPY 2006
- POJ 3090 Visible Lattice Points
- POJ-2409-Let it Bead&&NYOJ-280-LK的项链
- POJ-1695-Magazine Delivery-dp
- POJ1523 SPF dfs
- POJ-1001 求高精度幂-大数乘法系列
- POJ-1003 Hangover
- POJ-1004 Financial Management
- [数论]poj2635__The Embarrassed Cryptographer
- [二分图匹配]poj2446__Chessboard
- POJ1050 最大子矩阵和
- 用单调栈解决最大连续矩形面积问题