poj 3159 Candies
来源:互联网 发布:linux怎么重置root密码 编辑:程序博客网 时间:2024/06/01 15:22
poj 3159 Candies
Time Limit: 1500MS Memory Limit: 131072K
Total 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 2
1 2 5
2 1 4
Sample Output
5
Hint
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[N];}
源代码
#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[N];}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;}
- poj 3159 Candies
- POJ 3159 Candies
- poj 3159 Candies
- POJ 3159 candies总结
- POJ 3159 Candies
- POJ 3159 Candies
- poj 3159-Candies
- POJ-3159-Candies
- POJ 3159 Candies
- POJ 3159 Candies
- poj 3159 Candies
- POJ 3159 Candies
- poj 3159 Candies
- POJ -- 3159 Candies
- poj 3159 Candies
- Candies ( POJ 3159)
- poj 3159 Candies
- POJ 3159 Candies
- 可重入函数与线程安全
- Codeforces 66 D Petya and His Friends(找规律)
- 在线赛模板
- spring 需要导入的jar
- 【问题解决】android网络连接—主线程连接错误
- poj 3159 Candies
- Linux vim大全
- 使用cocos2d-android做最简单的飞机大战
- 一点一点学maven(01)——maven介绍、下载、安装及配置
- poj1936 All in All
- MapReduce入门介绍
- Android 属性动画(Property Animation) 完全解析 (上)
- Oracle 11g R2 DBA 操作指南(6)
- 设置Button背景色与点击涟漪颜色(5.0+)