[POJ 3268 Silver Cow Party ]Dijkstra
来源:互联网 发布:淘宝店家虚假发货 编辑:程序博客网 时间:2024/06/05 20:13
[POJ 3268 Silver Cow Party ]Dijkstra
知识点:Dijkstra
1. 题目链接
[POJ 3268 Silver Cow Party ]
2. 题意描述
有
3. 解题思路
原图从x开始用dijkstra求一次最短路。然后将原图反向,再从x开始dijkstra求一次最短路。两次最短路累加求和,取最大值就是答案。
4. 实现代码
// C#ifndef _GLIBCXX_NO_ASSERT#include <cassert>#endif#include <cctype>#include <cerrno>#include <cfloat>#include <ciso646>#include <climits>#include <clocale>#include <cmath>#include <csetjmp>#include <csignal>#include <cstdarg>#include <cstddef>#include <cstdio>#include <cstdlib>#include <cstring>#include <ctime>#if __cplusplus >= 201103L#include <ccomplex>#include <cfenv>#include <cinttypes>#include <cstdalign>#include <cstdbool>#include <cstdint>#include <ctgmath>#include <cwchar>#include <cwctype>#endif// C++#include <algorithm>#include <bitset>#include <complex>#include <deque>#include <exception>#include <fstream>#include <functional>#include <iomanip>#include <ios>#include <iosfwd>#include <iostream>#include <istream>#include <iterator>#include <limits>#include <list>#include <locale>#include <map>#include <memory>#include <new>#include <numeric>#include <ostream>#include <queue>#include <set>#include <sstream>#include <stack>#include <stdexcept>#include <streambuf>#include <string>#include <typeinfo>#include <utility>#include <valarray>#include <vector>#if __cplusplus >= 201103L#include <array>#include <atomic>#include <chrono>#include <condition_variable>#include <forward_list>#include <future>#include <initializer_list>#include <mutex>#include <random>#include <ratio>#include <regex>#include <scoped_allocator>#include <system_error>#include <thread>#include <tuple>#include <typeindex>#include <type_traits>#include <unordered_map>#include <unordered_set>#endifusing namespace std;typedef long long LL;typedef long double LB;typedef pair<int, int> PII;typedef pair<LL, LL> PLL;const int INF = 0x3f3f3f3f;const LL INFL = 0x3f3f3f3f3f3f3f3fLL;const LB eps = 1e-8;const int MAXN = 1000 + 5;const int MAXM = 100000 + 5;template <typename T>inline bool scan_d (T &ret) { char c; int sgn; if (c = getchar(), c == EOF) return 0; //EOF while (c != '-' && (c < '0' || c > '9') ) c = getchar(); sgn = (c == '-') ? -1 : 1; ret = (c == '-') ? 0 : (c - '0'); while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0'); ret *= sgn; return 1;}template<typename T>void print(T x) { static char s[33], *s1; s1 = s; if (!x) *s1++ = '0'; if (x < 0) putchar('-'), x = -x; while(x) *s1++ = (x % 10 + '0'), x /= 10; while(s1-- != s) putchar(*s1);}template<typename T> void println(T x) { print(x); putchar('\n');}int n, m, x;struct Dijkstra { struct Edge { int v, w, next; Edge() {} Edge (int v, int w, int next) : v (v), w (w), next (next) {} } edges[MAXM]; struct QNode { int u, w; QNode() {} QNode (int u, int w) : u (u), w (w) {} bool operator > (const QNode& e) const { return w > e.w; } } cur; int head[MAXN], tot; int dist[MAXN]; bool vis[MAXN]; priority_queue<QNode, vector<QNode>, greater<QNode> > Q; void init() { tot = 0; memset(head, -1, sizeof (head) ); memset(dist, 0x3f, sizeof (dist) ); memset(vis, false, sizeof (vis) ); } void add_edge(int u, int v, int w) { edges[tot] = Edge (v, w, head[u]); head[u] = tot ++; } void run(int src) { int u, v, w; Q.push(QNode (src, dist[src] = 0) ); while(!Q.empty() ) { cur = Q.top(); Q.pop(); u = cur.u; if(vis[u]) continue; vis[u] = true; for(int i = head[u]; ~i; i = edges[i].next) { v = edges[i].v, w = edges[i].w; if(!vis[v] && dist[v] > dist[u] + w) { dist[v] = dist[u] + w; Q.push(QNode(v, dist[v])); } } } }} dij[2];int main() {#ifdef ___LOCAL_WONZY___ freopen("input.txt", "r", stdin);#endif // ___LOCAL_WONZY___ int u, v, w; while(scan_d(n) && scan_d(m) && scan_d(x)) { dij[0].init(); dij[1].init(); for(int i = 1; i <= m; ++i) { scan_d(u), scan_d(v), scan_d(w); dij[0].add_edge(u, v, w); dij[1].add_edge(v, u, w); } dij[0].run(x); dij[1].run(x); int ans = 0; for(int i = 1; i <= n; ++i) { if(i == x) continue; if(dij[0].dist[i] == INF || dij[1].dist[i] == INF) continue; ans = max(ans, dij[0].dist[i] + dij[1].dist[i]); } println(ans); } return 0;}
0 0
- POJ 3268 Silver Cow Party(Dijkstra算法)
- POJ 3268 - Silver Cow Party(dijkstra)
- POJ 3268 Silver Cow Party (Dijkstra~)
- POJ 3268 Silver Cow Party(dijkstra)
- poj 3268 Silver Cow Party , spfa , dijkstra
- POJ 3268 Silver Cow Party(Dijkstra)
- Dijkstra-POJ-3268-Silver Cow Party
- POJ 3268Silver Cow Party dijkstra();
- [POJ 3268 Silver Cow Party ]Dijkstra
- POJ - 3268----Silver Cow Party(Dijkstra)
- poj 3268 Silver Cow Party【dijkstra】
- POJ.3268 Silver Cow Party (Dijkstra)
- POJ 3268 Silver Cow Party(Dijkstra)
- poj 3268 Silver Cow Party Dijkstra 和SPFA
- poj 3268 Silver Cow Party dijkstra基础题!!!入门
- poj 3268 Silver Cow Party 最短路/dijkstra
- POJ 3268 Silver Cow Party 2个dijkstra
- POJ 3268 Silver Cow Party dijkstra单源最短路
- Git标签的使用
- HDU2389Rain on your Parade 二分匹配Hopcroft-Karp
- 自定义view快速入门--基础概念
- java---构造方法
- 2016总结
- [POJ 3268 Silver Cow Party ]Dijkstra
- 存储过程,存储函数,触发器。。。
- member access within misaligned address 0x000000000031 for type 'struct ListNode', which requires 8
- Jenkins+Gradle+findbugs对Android工程源码进行静态代码分析
- HDU5546 Ancient Go(DFS)
- Matlab坐标轴操作汇总
- jQuery中设置form表单中action值的方法
- 新年简单谈谈java异常
- 【Linux学习】之 磁盘及文件系统管理