Time: 1000 ms/Space: 131072kib/Java class name: Main background usaco oct09 9th description Texas simple people 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 ).
[1] ---- 1 --- [3]-
/\
[3] --- 6 --- [4] --- 3 -- [3] -- 4
/// |
5 -- [3] -- [2]-|
\\// |
[5] --- 7 --- [2] -- 2 --- [3] ---
|/
[1] ------
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 * First line: A separate integer represents the shortest length of TS to te. (Not cost? How can I suddenly become straightforward?
-- Translator's note) There must be at least one path to data assurance. Test 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
Output
7
Note 5-> 6-> 1-> 4 (3 + 1 + 3) Ideas
Spfa...
Code
#include<iostream>#include<cstdlib>#include<cstdio>using namespace std;int p[3000][3000];int p1,p2;int head,tail,T,C,start,end;int now,nowstep;int queue[620005];int least[3000];int main(){ cin>>T>>C>>start>>end; for (int i=1;i<=T;i++) for (int j=1;j<=T;j++) p[i][j]=598460606; for (int i=1;i<=C;i++) { cin>>p1>>p2; cin>>p[p1][p2]; p[p2][p1]=p[p1][p2]; } for (int i=1;i<=T;i++) least[i]=598460606; head=1;tail=1;queue[1]=start;least[start]=0; while (head<=tail) { now=queue[head]; for (int i=1;i<=T;i++) { if ( least[i]> (least[now]+p[now][i]) ) { least[i]=least[now]+p[now][i]; tail++; queue[tail]=i; } } head++; } cout<<least[end]<<endl; return 0; }
Result
[Spfa] tyvj1031 heat wave and usaco Heat Wave