POJ 3268 Silver Cow Party(dijkstra单源最短路)

裸 dijkstra
思路:以x为源点,求到其他点的最短路,之后把邻接矩阵转置,再求一次x源

点的最短路,这样就一次是来的,一次是走的,相加迭代最大值即可

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
poj 3268
8108K 47MS
*/
#include<cstdio>
#include<iostream>

#define MAXN 1005
#define MAX_INT 2147483647

using namespace std;

int gra_in[MAXN][MAXN],gra_out[MAXN][MAXN],n,m,p,dist_in[MAXN],dist_out[MAXN];

void init()
{
cin>>n>>m>>p;
for(int i = 1;i <= m;i ++)
{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
gra_out[a][b] = c;
gra_in[b][a] = c;
}
}

void dijkstra(int gra[MAXN][MAXN] , int dist[MAXN])
{
bool mark[MAXN] = {false};
for(int i = 1;i <= n;i ++)
dist[i] = MAX_INT;
dist[p] = 0;
for(int i = 1;i <= n;i ++)
{
int Min = MAX_INT,tj;
for(int j = 1;j <= n;j ++)
{
if(mark[j])
continue;
if(Min > dist[j])
{
Min = dist[j];
tj = j;
}
}
mark[tj] = true;
for(int j = 1;j <= n;j ++)
{
if(mark[j] || !gra[tj][j])
continue;
dist[j] = min(dist[j] , dist[tj] + gra[tj][j]);
}
}
}

int main()
{
init();
dijkstra(gra_in , dist_in);
dijkstra(gra_out , dist_out);
int Max = 0;
for(int i = 1;i <= n;i ++)
Max = max(Max , dist_in[i] + dist_out[i]);
cout<<Max<<endl;
return 0;
}

POJ 3268 Silver Cow Party(dijkstra单源最短路)

https://rucer.cn/2014-05/poj-3268/

作者

Ferris Tien

发布于

2014-05-26

更新于

2024-10-19

许可协议