poj 3268 Silver Cow Party 【最短路Dijkstra + 结构体妙用】
2015-08-05 15:53
477 查看
Silver Cow Party
Submit Status
Description
One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional
(one-way roads connects pairs of farms; road i requires Ti (1 ≤ Ti ≤ 100) units of time to traverse.
Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow's return route might be different from her original route to the party since roads are one-way.
Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?
Input
Line 1: Three space-separated integers, respectively: N, M, and X
Lines 2.. M+1: Line i+1 describes road i with three space-separated integers: Ai, Bi, and Ti. The described road runs from farm Ai to farm Bi,
requiring Ti time units to traverse.
Output
Line 1: One integer: the maximum of time any one cow must walk.
Sample Input
Sample Output
Hint
Cow 4 proceeds directly to the party (3 units) and returns via farms 1 and 3 (7 units), for a total of 10 time units.
题意:给定N个顶点,M条边,构成一个单向图,求从图上某点到 X点 (已知)来回的最大最短路。
分析:题目很水,一次是固定终点,一次是固定起点,终点固定可以看成固定起点同时把边反向,两次Dijkstra,求和就可以了。但是这里有一个结构体的妙用,可以简化代码,而且用起来超级方便,给推荐~~~
Time Limit: 2000MS | Memory Limit: 65536KB | 64bit IO Format: %I64d & %I64u |
Description
One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional
(one-way roads connects pairs of farms; road i requires Ti (1 ≤ Ti ≤ 100) units of time to traverse.
Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow's return route might be different from her original route to the party since roads are one-way.
Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?
Input
Line 1: Three space-separated integers, respectively: N, M, and X
Lines 2.. M+1: Line i+1 describes road i with three space-separated integers: Ai, Bi, and Ti. The described road runs from farm Ai to farm Bi,
requiring Ti time units to traverse.
Output
Line 1: One integer: the maximum of time any one cow must walk.
Sample Input
4 8 2 1 2 4 1 3 2 1 4 7 2 1 1 2 3 5 3 1 2 3 4 4 4 2 3
Sample Output
10
Hint
Cow 4 proceeds directly to the party (3 units) and returns via farms 1 and 3 (7 units), for a total of 10 time units.
题意:给定N个顶点,M条边,构成一个单向图,求从图上某点到 X点 (已知)来回的最大最短路。
分析:题目很水,一次是固定终点,一次是固定起点,终点固定可以看成固定起点同时把边反向,两次Dijkstra,求和就可以了。但是这里有一个结构体的妙用,可以简化代码,而且用起来超级方便,给推荐~~~
/****************************>>>>HEADFILES<<<<****************************/ #include <cmath> #include <queue> #include <vector> #include <cstdio> #include <string> #include <cstring> #include <iomanip> #include <iostream> #include <algorithm> using namespace std; /****************************>>>>>DEFINE<<<<<*****************************/ //#pragma comment(linker, "/STACK:1024000000,1024000000") #define FIN freopen("input.txt","r",stdin) #define rep(i,a,b) for(int i = a;i <= b;i++) #define rep0(i,b) for(int i = 0;i < b;i++) #define rep1(i,b) for(int i = 1;i <= b;i++) #define MP(a,b) make_pair(a,b) #define PB(a) push_back(a) #define fst first #define snd second /****************************>>>>>>DEBUG<<<<<<****************************/ #define out(x) cout<<x<<" "; /****************************>>>>SEPARATOR<<<<****************************/ const int maxn = 1000 + 5; const int INF = 0x3f3f3f3f; int N,M,X; struct Edge { int from, to, dist; Edge() {} Edge(int _from, int _to, int _dist) : from(_from), to(_to), dist(_dist) {} }; struct HeapNode { int pos, d; HeapNode() {} HeapNode(int _pos, int _d) : pos(_pos), d(_d) {} bool operator < (const HeapNode & p) const { return d > p.d; } }; struct Dijkstra { int n, s, d[maxn]; bool vis[maxn]; vector<Edge> edges; vector<int> G[maxn]; Dijkstra() {} Dijkstra(int _n, int _s) : n(_n), s(_s) {} void init(int _n, int _s) { this->n = _n, this->s = _s; edges.clear(); rep1(i, n) { G[i].clear(); } } void AddEdge(int u, int v, int t) { edges.PB(Edge(u, v, t)); int m = edges.size(); G[u].PB(m - 1); } void dijkstra() { memset(vis, false, sizeof(vis)); rep1(i, n) d[i] = (i == s ? 0 : INF); priority_queue<HeapNode> Que; Que.push(HeapNode(s, 0)); while(!Que.empty()) { HeapNode now = Que.top(); Que.pop(); int u = now.pos; if(vis[u]) continue; vis[u] = true; for(int i = 0; i < G[u].size(); i++) { Edge& e = edges[G[u][i]]; if(d[e.to] > d[u] + e.dist) { d[e.to] = d[u] + e.dist; Que.push(HeapNode(e.to,d[e.to])); } } } } }dij1,dij2; int main() { // FIN; while(~scanf("%d %d %d",&N,&M,&X)) { dij1.init(N,X);dij2.init(N,X); int a,b,t; rep(i,1,M) { scanf("%d %d %d",&a,&b,&t); dij1.AddEdge(b,a,t); dij2.AddEdge(a,b,t); } dij1.dijkstra(); dij2.dijkstra(); int ans = 0; rep1(i,N) ans = max(ans,dij1.d[i] + dij2.d[i]); printf("%d\n",ans); } return 0; }
相关文章推荐
- 关于jsp页面中中文乱码问题的解决方法
- html的标签
- 输出是m的倍数或n的倍数、但不是m和n的公倍数的数
- Activiti安装配置(转)
- MTK+Android编译
- NSString 查找指定字符串出现的次数
- SEO在网页制作中的应用笔记
- 华为软件编程规范和范例 7 —— 代码编辑、编译、审查和代码测试、维护以及宏
- 南邮 OJ 1443 Warcraft III
- CDN
- linux和STL 常用头文件及说明
- 图像处理文献资料
- 每天一道算法题(30)——高效的求斐波拉契数列
- MTK+Android编译
- 每天一道算法题(30)——高效的求斐波拉契数列
- C++ 拷贝构造函数 和 六大函数
- AngularJS页面加载闪烁解决方案
- windows系统 远程桌面限制单用户
- struts的bean标签用法
- 九度oj 1162