Silver Cow Party
总时间限制: 2000ms
内存限制: 65536
题目描述
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?
输入格式:
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.
输出格式:
Line 1: One integer: the maximum of time any one cow must walk.
样例输入
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
样例输出
10
提示
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.
题意
求有向图求各点到X和X到各点的最短路径之和中的最大值
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#define maxn 1000 + 5
#define INF 0x3f3f3f3f
#define xin(a) scanf("%d", &a)
#define ffr(i, m, n) for ( i = m; i < n; i++ )
#define clr(a, b) memset(a, b, sizeof(a));
#define pi acos(-1, 0)
typedef double db;
typedef long long ll;
using namespace std;
int dis[maxn]; //最小路径权值
int vis[maxn]; //是否访问过
int path[maxn]; //存储路径
int G[maxn][maxn]; //权值信息
int point, edge, zd; // point个坐标,edge条边
void Dijkstra(int star, int end)
{
for ( int i = 1; i <= point; i++ )
{
vis[i] = 0;
dis[i] = G[star][i];
}
for ( int i = 1; i <= point; i++ )
{ //遍历全部点
int x, minn = INF;
for ( int j = 1; j <= point; j++ )
{ //找出距离未访问点的最小距离和该点
if ( !vis[j] && dis[j] <= minn )
{
x = j;
minn = dis[j];
}
}
vis[x] = 1;
for ( int j = 1; j <= point; j++ )
{ //更新未访问点经过最小距离点后的最小权值
if ( !vis[j] && dis[x] + G[x][j] < dis[j] ) { dis[j] = dis[x] + G[x][j]; }
}
}
}
void init() //初始化图
{
int i, j, a, b, c;
cin >> point >> edge >> zd;
for ( i = 1; i <= point; i++ )
for ( j = 1; j <= point; j++ )
if ( i == j ) G[i][j] = 0;
else
{
G[i][j] = INF;
}
for ( i = 0; i < edge; i++ )
{
cin >> a >> b >> c;
G[a][b] = min(c, G[a][b]);
}
}
void n(void)
{
int n, i, d = -1, j, sum = 0;
init();
Dijkstra(zd, 1);
for ( i = 1; i <= point; i++ ) {
path[i] = dis[i];
}
for ( i = 1; i <= point; i++ )
for ( j = i+1; j <= point; j++ ) swap(G[i][j], G[j][i]);
Dijkstra(zd, i);
for ( i = 1; i <= point; i++ )
{
path[i] += dis[i];
d = max(d, path[i]);
}
cout << d << endl;
}
int main(void)
{
#ifndef ONLINE_JUDGE
long _begin_time = clock();
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
n();
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms\n", _end_time - _begin_time);
fclose(stdin);
fclose(stdout);
#endif
return 0;
}
解题思路
x到各点的最短路径由Dijkstra算法求出,第一次提交直接用了n次求n到x最短路径,不出意料的超时.经过查询各点到x的最短路径只需将有向图翻转,仍可由Dijkstra求出,求出x到各点最短路径,也就是各点到x的最短路径,即可.

浙公网安备 33010602011771号