POJ 3268 Silver Cow Party
来源:互联网 发布:软件测试金字塔 编辑:程序博客网 时间:2024/04/30 13:44
给出n个点和m条边,接着是m条边,代表从牛a到牛b需要花费c时间,现在所有牛要到牛x那里去参加聚会,并且所有牛参加聚会后还要回来,给你牛x,除了牛x之外的牛,他们都有一个参加聚会并且回来的最短时间,从这些最短时间里找出一个最大值输出。
最短路径只需要从x到i的最短路径代表他们返回的最短路径,然后将所有边反过来,再从x到i的最短路径代表他们来参加聚会的最短路径,这样对应相加找出一个最大值就可以了,当然其实不需要将所有边反过来。
反向找路径,很有意思的一道题,用邻接表超快,先给出我的第一个版本
//384K47MS#include <cstdio>#include <iostream>#include <cstring>#include <queue>#include <vector>using namespace std;typedef pair<int, int> pii;int n, m, x;const int maxn = 1000;const int maxm = 100000;const int inf = 0x7fffffff;vector<pii> G1[maxn+5];vector<pii> G2[maxn+5];int d1[maxn+5], d2[maxn+5];void Dijkstra(vector<pii> G[], int d[], int s){ priority_queue< pii, vector<pii>, greater<pii> > pque; fill(d+1, d+n+1, inf); d[s] = 0; pque.push(pii(0, s)); while(!pque.empty()){ pii t = pque.top(); pque.pop(); int v = t.second; if(d[v] < t.first) continue; int len = G[v].size(); for(int i=0; i<len; i++){ pii e = G[v][i]; if(d[e.second] > t.first + e.first){ d[e.second] = t.first + e.first; pque.push(pii(d[e.second], e.second)); } } }}int main(){ while(~scanf("%d%d%d", &n, &m, &x)){ int a, b, t; for(int i=1; i<=m; i++){ scanf("%d%d%d", &a, &b, &t); G1[a].push_back(pii(t, b)); G2[b].push_back(pii(t, a)); } Dijkstra(G1, d1, x); Dijkstra(G2, d2, x); int res = 0; for(int i=1; i<=n; i++){ if(d1[i]+d2[i] > res) res = d1[i]+d2[i]; } printf("%d\n", res); } return 0;}
下面是我自定义优先级的比较函数,没有用pair
//384K79MS#include <cstdio>#include <iostream>#include <cstring>#include <queue>#include <vector>using namespace std;struct node{ int cost, to; node(int cost=0, int to=0) : cost(cost), to(to) { } bool operator < (const node& j) const { return cost > j.cost; }};int n, m, x;const int maxn = 1000;const int maxm = 100000;const int inf = 0x7fffffff;vector<node> G1[maxn+5];vector<node> G2[maxn+5];int d1[maxn+5], d2[maxn+5];void Dijkstra(vector<node> G[], int d[], int s){ priority_queue<node> pque; fill(d+1, d+n+1, inf); d[s] = 0; pque.push(node(0, s)); while(!pque.empty()){ node t = pque.top(); pque.pop(); int v = t.to; if(d[v] < t.cost) continue; int len = G[v].size(); for(int i=0; i<len; i++){ node n = G[v][i]; if(d[n.to] > t.cost + n.cost){ d[n.to] = t.cost + n.cost; pque.push(node(d[n.to], n.to)); } } }}int main(){ while(~scanf("%d%d%d", &n, &m, &x)){ int a, b, t; for(int i=1; i<=m; i++){ scanf("%d%d%d", &a, &b, &t); G1[a].push_back(node(t, b)); G2[b].push_back(node(t, a)); } Dijkstra(G1, d1, x); Dijkstra(G2, d2, x); int res = 0; for(int i=1; i<=n; i++){ if(d1[i]+d2[i] > res) res = d1[i]+d2[i]; } printf("%d\n", res); } return 0;}
0 0
- 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
- 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
- 文件断点续传原理
- 数位DP入门篇之HDU2089——不要62
- ios之音频转换:amr转换wav(安卓amr是常用格式,wav是ios格式)
- MyEclipse异常:Class file name must end with .class
- 菜鸟调MAX5825B
- POJ 3268 Silver Cow Party
- hdu 1995 汉诺塔V(递推)
- Ubuntu14.04 64位出现grub rescue解决办法
- The NPF driver isn't running.You may have trouble capturing or listing interfaces
- android.graphics.Bitmap.Config
- Reorder List--简单粗暴的水题
- hdu 4864
- 访问google、YouTube的host配置
- Jquery的$命名冲突