Background
USACO OCT09 9TH
Description
Simple Texas citizens are experiencing a huge heat wave this summer !!! They have a good meal, but they are not very good at producing Cream-rich milk products. Farmer John was at this time with the joys and sorrows of the world, taking on the task of transporting a large amount of nutritious, cold milk to Texas, to relieve Texas people from suffering from the heat.
FJ has studied how milk can be shipped from Wisconsin to Texas. These routes include the starting point and the ending point first passing through T (1 <= T <= 2,500) towns, which are conveniently marked as 1 to T. Each town except the start and end areas is connected by two-way roads to at least two other towns. There is a pass-through fee for each road (including the fuel fee, the toll, and so on ). Consider a map of seven towns. Town 5 is the milk source, and town 4 is the destination (The number in parentheses is the cost of passing the road ).
The total cost of Route 5-6-4 is 3 (5-> 6) + 4 (6-> 3) + 3 (3-> 4) = 10.
Given a map, it contains C (1 <= C <= 6,200) roads that are directly connected to two towns. Start Point Rs of each road, end point Re (1 <= Rs <= T; 1 <= Re <= T), and cost (1 <= Ci <= 1,000). Calculate the minimum total cost of the town Te (1 <= Ts <= T) from the start point to the end point.
Input Format
* First line: Four integers separated by spaces: T, C, Ts, Te
* Line 2nd to line C + 1: line I + 1 describes the road under line I. There are three integers separated by spaces: Rs, Re, and Ci
Output Format
* Line 1: A separate integer represents the shortest length between Ts and Te. (Not cost? How can I suddenly become straightforward?
-- Translator's note) There must be at least one path to data assurance.
Sample Input
7 11 5 4
2 4 2
1 4 3
7 2 2
3 4 3
5 7 5
7 3 3
6 1 1
6 3 4
2 4 3
5 6 3
7 2 1
Sample Output
7
Time Limit
1 s for each test point
Comment Hint
5-> 6-> 1-> 4 (3 + 1 + 3)
Analysis:
Apparently, I used dijstra a long time ago,
Now I am a beginner in spfa. I will use it to train my hands and basic algorithms. I don't want to explain it anymore...
Dijstra:
program ty_1031; var i,j,n,m,t,k,l,ss,s,cc,c,ee,e,min,minn:longint; a:array[1..2500,1..2500]of longint; b:array[1..2500]of integer; begin assign(input,'ty.in'); reset(input); readln(t,c,ss,ee); filldword(a,sizeof(a)div 4,maxint); for i:=1 to c do begin readln(s,e,cc); if cc< a[s,e] then begin a[s,e]:=cc; a[e,s]:=cc; end; end; minn:=ss;b[ss]:=1; for k:=2 to t do begin min:=maxlongint; for i:=1 to t do if (a[ss,i]<min)and(b[i]=0) then begin min:=a[ss,i]; minn:=i; end; b[minn]:=1; for j:=1 to t do if (j<>minn)and(a[ss,minn]+a[minn,j]<a[ss,j])and(b[j]=0) then a[ss,j]:=a[ss,minn]+a[minn,j]; end; writeln(a[ss,ee]); close(input); end.
Spfa:
program ty_1031_2; var i,j,n,m,k,l,num,st,en,x,y,z,zz,head,tail,nn:longint; a:array[0..20000]of record f,t,val,next:longint; end; b,d:array[1..20000]of longint; q:array[1..100000]of longint; begin assign(input,'ty.in'); reset(input); readln(n,m,st,en); num:=3000; for i:=1 to m do begin read(x,y,z); if a[x].val=0 then begin a[x].t:=y;a[x].val:=z;a[x].f:=x;end else begin zz:=x; while a[zz].next<>0 do zz:=a[zz].next; inc(num); a[zz].next:=num; a[num].val:=z; a[num].f:=x; a[num].t:=y; end; if a[y].val=0 then begin a[y].t:=x;a[y].val:=z;a[y].f:=y;end else begin zz:=y; while a[zz].next<>0 do zz:=a[zz].next; inc(num); a[zz].next:=num; a[num].val:=z; a[num].f:=y; a[num].t:=x; end; end; fillchar(d,sizeof(d),10); head:=1;tail:=1;b[st]:=1; q[1]:=st;d[st]:=0; while head<>tail+1 do begin nn:=q[head]; repeat if (a[nn].val+d[a[nn].f]<d[a[nn].t])then begin d[a[nn].t]:=a[nn].val+d[a[nn].f]; if b[a[nn].t]=0 then begin b[a[nn].t]:=1; inc(tail); if tail=100000 then tail:=1; q[tail]:=a[nn].t; end; end; nn:=a[nn].next; until nn=0; b[q[head]]:=0; inc(head); if head=100000 then tail:=1; end; writeln(d[en]); close(input); end.