1557 heat Wave
time limit: 1 sspace limit: 256000 KBtitle level: Diamonds Diamond SolvingView Run ResultsTitle Description
Description
Texas's simple people are suffering a huge heat wave this summer!!! Their Texas horned cows are good to eat, but they are not very good at producing cream-rich dairy products. Farmer John at this time first, enjoy spirit, Shenxianshizu to Texas to carry a large number of nutritious cold milk, to alleviate the Texan people endure the pain of heat.
FJ has studied the route of transporting milk from Wisconsin to Texas. These routes include a starting point and an end point that are preceded by a total of T (1 <= t <= 2,500) towns, conveniently labeled 1 to T. In addition to the beginning and end of the field each town is connected by two bidirectional roads to at least two other towns. Each road has a pass fee (including fuel, toll, etc.).
Given a map, the roads containing C (1 <= C <= 6,200) are directly connected to 2 towns. Each road consists of the beginning of the road RS, the end point re (1 <= Rs <= t; 1 <= Re <= t), and the cost (1 <= Ci <= 1,000). The minimum total cost for the town Te (1 <= te <= t) from the beginning of the town TS (1 <= TS <= t) to the end point.
Enter a description
Input Description
First line: 4 integers separated by a space: T, C, Ts, Te
2nd to C+1: line i+1 describes article I road. There are 3 integers separated by a space: Rs, RE and CI
Output description
Output Description
A single integer represents the minimum total cost from TS to TE. The data guarantees that there is at least one road.
Sample input
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
Sample Output
7
Data range and Tips
Data Size & Hint
5->6->1->4 (3 + 1 + 3)
Ideas:
Bare SPFA;
Come on, on the code:
#include <queue>#include<cstdio>#include<iostream>using namespacestd;structNode {intTo,dis,next;};structNode edge[12801];intnum_head,num_edge,num,start,end,head[2501],dis[2501];CharWord;voidSPFA (intAll_from) {Queue<int>que; BOOLif_in_spfa[2501]; for(intI=1; I<=num_head; i++) dis[i]=99990000, if_in_spfa[i]=false; Dis[all_from]=0; If_in_spfa[all_from]=true; Que.push (All_from); intcur_1; while(!Que.empty ()) {cur_1=Que.front (); Que.pop (); for(intI=head[cur_1]; I I=Edge[i].next) { if(dis[cur_1]+edge[i].dis<Dis[edge[i].to]) {Dis[edge[i].to]=dis[cur_1]+Edge[i].dis; if(!If_in_spfa[edge[i].to]) {If_in_spfa[edge[i].to]=true; Que.push (edge[i].to); }}} If_in_spfa[cur_1]=false; }}inlinevoidRead_int (int&now_001) {now_001=0; word=GetChar (); while(word<'0'|| Word>'9') word=GetChar (); while(word<='9'&&word>='0') {now_001=now_001*Ten+(int) (word-'0'); Word=GetChar (); }}inlinevoidEdge_add (int from,intTo,intdis) {num++; Edge[num].to=to ; Edge[num].dis=dis; Edge[num].next=head[ from]; head[ from]=num;}intMain () {Ios::sync_with_stdio (false); Read_int (Num_head), Read_int (Num_edge), Read_int (start), read_int (end); int from, To,dis1; for(intI=1; I<=num_edge; i++) {Read_int ( from), Read_int (To), Read_int (DIS1); Edge_add ( from, TO,DIS1); Edge_add (To, from, DIS1); } SPFA (start); printf ("%d\n", Dis[end]); return 0;}
AC diary--heat wave codevs 1557 (Shortest path template problem)