POJ 3268
2015-10-13 21:49
369 查看
#include<iostream> #include<cstdio> #include<vector> #include<queue> #include<algorithm> using namespace std; #define N 1200 #define inf 0x3f3f3f3f struct eg { int pos, val; eg(int pp, int vv) :pos(pp), val(vv){} eg(){} }; vector<eg>g1 ; vector<eg>g2 ; int vis , d1 , d2 ; void spfa1(int dest) { memset(d1, inf, sizeof(d1)); memset(vis, 0, sizeof(vis)); vis[dest] = 1; d1[dest] = 0; queue<int>q; q.push(dest); while (!q.empty()) { int x = q.front(); q.pop(); vis[x] = 0; for (int i = 0; i < g1[x].size(); i++) { int v = g1[x][i].pos; if (v == x)continue; int cost = g1[x][i].val; if (d1[v]>d1[x] + cost) { d1[v] = d1[x] + cost; if (!vis[v]) { q.push(v); vis[v] = 1; } } } } } void spfa2(int dest) { memset(d2, inf, sizeof(d2)); memset(vis, 0, sizeof(vis)); vis[dest] = 1; d2[dest] = 0; queue<int>q; q.push(dest); while (!q.empty()) { int x = q.front(); q.pop(); vis[x] = 0; for (int i = 0; i < g2[x].size(); i++) { int v = g2[x][i].pos; if (v == x)continue; int cost = g2[x][i].val; if (d2[v]>d2[x] + cost) { d2[v] = d2[x] + cost; if (!vis[v]) { q.push(v); vis[v] = 1; } } } } } int main() { #ifdef CDZSC freopen("in.txt", "r", stdin); #endif int n, m, des, x, y, v; while (~scanf("%d%d%d", &n, &m, &des)) { while (m--) { scanf("%d%d%d", &x, &y, &v); g1[x].push_back(eg(y, v)); g2[y].push_back(eg(x, v)); } spfa1(des); spfa2(des); int ans = -inf; for (int i = 1; i <= n; i++) ans=max(ans,d1[i]+d2[i]); printf("%d\n", ans); } return 0; }
题意是给你一个点des作为终点,你需要求出所有的点到des的最短路,再求出des到所有点的最短路,求在这来回的路径中最长的那条路。开始大家都会很容易想到用Floyd去求任意点的最短路,但是O(n^3)明显1s不能够承受这么大的复杂度。换个思路,将矩阵转秩。只需要求2次最短路就可以了。以上为代码。以下是SPFA模板.
vector<eg>g
;
int vis
,d
;
void spfa(int dest)
{
memset(d, inf, sizeof(d));
memset(vis, 0, sizeof(vis));
vis[dest] = 1;
d[dest] = 0;
queue<int>q;
q.push(dest);
while (!q.empty())
{
int x = q.front(); q.pop();
vis[x] = 0;
for (int i = 0; i < g[x].size(); i++)
{
int v = g[x][i].pos;
if (v == x)continue;
int cost = g[x][i].val;
if (d[v]>d[x] + cost)
{
d[v] = d[x] + cost;
if (!vis[v])
{
q.push(v);
vis[v] = 1;
}
}
}
}
}
Silver Cow Party
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 15910 | Accepted: 7248 |
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.
Source
USACO 2007 February Silver
相关文章推荐
- LeetCode OJ:Insertion Sort List (插入排序链表)
- ACMJAVA
- SVM-支持向量机算法概述
- UIApplication微解
- python学习-文本数据分析2(文本分类)
- nim IDE aporia的编译
- 【模板】高精度
- poj3259(spfa)
- uvaLive 3713
- 源码解析:3D推拉门式滑动菜单
- Windows命令启动MySQL“ 发生系统错误 5 ”
- 纸上得来终觉浅 绝知此事要躬行
- IOS请求H5页面、要求自定义agent判断是电脑、安卓还是iPhone登录
- DIV+CSS设计IE6浮动产生双倍距离
- 欢迎使用CSDN-markdown编辑器
- URAL 1221 Malevich Strikes Back! (暴力枚举)
- shell自学笔记2
- MySql命令大全
- datagridview的行全选
- LevelDB 整体架构