POJ 1511
题目大意:有一副有向图,求从节点1到达其他点和从所有节点到达1的最小总费用。
讨论:处理有的节点不能到达的情况?
解:建逆图然后2次spfa,有时间切切dijkstra+heap?这也算是黑历史了吧,让我对spfa产生恐惧的题目,今天总算ac了,原因是sum>maxlongint,可恶的是int64对数组的作用原理有异,导致又wa了几次,最后还是老老实实的用了for,当我以为错误还是出现在越界时,神奇的ac了,可恶的int64 T T(实际上是int64的数组应用, filldword无效!就算是置了一个很大的值也没有longint顶用)。
View Code
1 const
2 maxn=100{0000};
3 maxm=100{0000};
4 type
5 data=record
6 dest, next: longint;
7 cost: int64;
8 end;
9 var
10 idol, edge: array[1..maxm]of data;
11 vect, turn: array[1..maxn]of longint;
12 dist: array[1..maxn]of int64;
13 p, q, n, tot: int64;
14 ans: int64;
15 procedure add(x, y: longint; z : int64);
16 begin
17 inc(tot);
18 with edge[tot] do
19 begin
20 dest := y;
21 cost := z;
22 next := vect[x];
23 vect[x] := tot;
24 end;
25 with idol[tot] do
26 begin
27 dest := x;
28 cost := z;
29 next := turn[y];
30 turn[y] := tot;
31 end;
32 end;
33
34 procedure init;
35 var
36 i, j, u, v: longint;
37 w: int64;
38 begin
39 tot := 0;
40 ans := 0;
41 fillchar(vect, sizeof(vect), 0);
42 fillchar(turn, sizeof(turn), 0);
43 readln(p, q);
44 for i := 1 to q do
45 begin
46 readln(u, v, w);
47 add(u, v, w);
48 end;
49 end;
50
51 procedure spfa(b: longint);
52 var
53 q: array[1..maxn+11]of longint;
54 visit: array[1..maxn]of boolean;
55 i, u, tail, head: longint;
56 begin
57 fillchar(visit, sizeof(visit), 0);
58 for i := 1 to p do dist[i] := maxlongint {<< 10};
59 {this filldword' sizeof(dist) can not shr 3, shr 2 was AC!}
60 head := 0;
61 tail := 1;
62 q[1] := b;
63 visit[b] := true;
64 dist[b] := 0;
65 while head<>tail do
66 begin
67 head := head mod (maxn+11) +1;
68 u := q[head];
69 i := vect[u];
70 while i<>0 do
71 with edge[i] do
72 begin
73 if dist[u] + cost < dist[dest] then
74 begin
75 dist[dest] := dist[u] + cost;
76 if not visit[dest] then
77 begin
78 visit[dest] := true;
79 tail := tail mod (maxn+11) +1;
80 q[tail] := dest;
81 end;
82 end;
83 i := next;
84 end;
85 visit[u] := false;
86 end;
87 end;
88
89 procedure main;
90 var
91 i, j: longint;
92 begin
93 readln(n);
94 for i := 1 to n do
95 begin
96 init;
97 spfa(1);
98 for j := 1 to p do ans := ans + dist[j];
99 edge := idol;
100 vect := turn;
101 spfa(1);
102 for j := 1 to p do ans := ans + dist[j];
103 writeln(ans);
104 end;
105 end;
106
107 begin
108 assign(input,'aaa.in'); reset(input);
109 main;
110
111 close(input);
112
113 end.