POJ 3268 Silver Cow Party [图论.最短路] 《挑战程序设计竞赛》2.5
来源:互联网 发布:超市数据分析报告格式 编辑:程序博客网 时间:2024/06/10 20:35
2017-05-23
题目大意:
N头牛,分别住在N个不同的牧场,牧场标号1~N。这N个牧场之间有M条单向路。现在要在第X个牧场开Party,每头牛都会从各自的牧场沿最短路到X,然后再从X沿最短路回去。因为这里是单向路,所以来的路和回的路可能是不同的。现在要求所有的牛中,走路走的最长的那头牛走的路的长度。
题解:
需要求X牧场到所有其他牧场的最短距离,和所有其他牧场到X牧场的最短距离。
第一次写用的Floyd-Warshall 算法,超时。
实际上我们并不需要任意一对顶点之间的最短距离。我们用邻接表储存图G, 所以只需要对G调用Dijkstra计算X牧场到所有其他牧场的最短距离,在对G的反向图revG调用Dijkstra计算所有其他牧场到X牧场的最短距离。
代码:
#include <iostream>#include <queue>#include <vector>#define MAXN 1010#define INF 0x3f3f3f3fusing namespace std;typedef pair<int, int> P;struct Edge { int to, cost; Edge(int to, int cost) { this->to = to; this->cost = cost; }};vector<Edge> G[MAXN];vector<Edge> revG[MAXN];int N, M, X;int d[MAXN];int revd[MAXN];void dijkstra(vector<Edge> *G, int *d, int s) { fill(d, d+MAXN, INF); d[s] = 0; priority_queue<P, vector<P>, greater<P> > que; que.push(P(0, s)); while (!que.empty()) { P p = que.top(); que.pop(); int v = p.second; for (int i = 0; i < G[v].size(); i++) { Edge e = G[v][i]; if (d[v] + e.cost < d[e.to]) { d[e.to] = d[v] + e.cost; que.push(P(d[e.to], e.to)); } } }}int main() { ios::sync_with_stdio(false); cin >> N >> M >> X; for (int i = 0; i < M; i++) { int u, v, w; cin >> u >> v >> w; G[u].push_back(Edge(v, w)); revG[v].push_back(Edge(u, w)); } dijkstra(G, d, X); dijkstra(revG, revd, X); int ans = 0; for (int i = 1; i <= N; i++) { ans = max(ans, d[i] + revd[i]); } cout << ans << endl; return 0;}
阅读全文
1 0
- POJ 3268 Silver Cow Party [图论.最短路] 《挑战程序设计竞赛》2.5
- poj 3268 Silver Cow Party(最短路)
- POJ 3268 Silver Cow Party 最短路
- POJ 3268 Silver Cow Party 最短路
- POJ 3268 Silver Cow Party 最短路
- POJ 3268 Silver Cow Party 最短路
- POJ 3268 Silver Cow Party 最短路
- poj 3268 Silver Cow Party(最短路)
- poj 3268 Silver Cow Party 图论 最短路
- poj 3268 Silver Cow Party(最短路+SPFA)
- poj 3268 Silver Cow Party 最短路/dijkstra
- poj 3268 Silver Cow Party(dijkstra最短路)
- POJ 3268 Silver Cow Party 最短路 dij
- POJ 3268 - Silver Cow Party(最短路dijkstra)
- POJ 3268 Silver Cow Party(最短路dijkstra)
- POJ 3268 Silver Cow Party (dijkstra来回最短路)
- POJ 3268Silver Cow Party bfs最短路
- POJ 3268 Silver Cow Party (来回最短路 SPFA)
- iOS学习之路08
- mysql 增删改查排序分页最简单的语法
- Esri wkt字符串转proj.4 字符串的方法
- OTT-TV网络电视EPG录制和EPG抓取解决方案
- bzoj1592 [Usaco2008 Feb]Making the Grade 路面修整 dp
- POJ 3268 Silver Cow Party [图论.最短路] 《挑战程序设计竞赛》2.5
- 遇到的各种坑
- JDK不配置环境变量也可以使用的方法(偏门)
- 99-IPv4 地址
- 应用架构演进
- 源文件名长度大于文件系统支持的长度。请尝试将其移动到具有较短路径名称的位置,或者在执行此操作前尝试将其重命名为较短的名称--解决办法
- python中的字典
- iOS学习之路09
- 模拟退火学习笔记