1557 heat wave, k1557
1557 Heat Wave
Time Limit: 1 s space limit: 256000 KB title level: Diamond Title Description
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 ).
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 description
Input Description
Line 1: Four integers separated by spaces: T, C, Ts, Te
Lines 2nd to C + 1: I + 1 Describe the I-th Road. There are three integers separated by spaces: Rs, Re, and Ci
Output description
Output Description
A separate integer represents the minimum total cost from Ts to Te. There must be at least one path to data assurance.
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 prompt
Data Size & Hint
5-> 6-> 1-> 4 (3 + 1 + 3)
CATEGORY tag
Tags click here to expandNo tags
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<queue> 5 using namespace std; 6 const int MAXN=30001; 7 const int maxn=0x7fffffff; 8 struct node 9 {10 int u;11 int v;12 int w;13 int next;14 }edge[MAXN]; 15 int num=1;16 int head[MAXN];17 int n,m,begin,end;18 int dis[MAXN];19 int vis[MAXN];20 void spfa()21 {22 for(int i=1;i<=n;i++)dis[i]=maxn;23 queue<int>q;24 vis[begin]=1;25 q.push(begin);26 dis[begin]=0;27 while(q.size()!=0)28 {29 int p=q.front();30 q.pop();31 vis[p]=0;32 for(int i=head[p];i!=-1;i=edge[i].next)33 {34 if(dis[edge[i].v]>dis[p]+edge[i].w&&dis[p]!=maxn)35 {36 dis[edge[i].v]=dis[p]+edge[i].w;37 if(vis[edge[i].v]==0)38 {39 q.push(edge[i].v);40 vis[edge[i].v]=1;41 }42 }43 }44 }45 printf("%d",dis[end]);46 }47 int main()48 {49 scanf("%d%d%d%d",&n,&m,&begin,&end);50 for(int i=1;i<=n;i++)head[i]=-1;51 for(int i=1;i<=m;i++)52 {53 scanf("%d%d%d",&edge[num].u,&edge[num].v,&edge[num].w);54 edge[num].next=head[edge[num].u];55 head[edge[num].u]=num++;56 edge[num].w=edge[num-1].w;57 edge[num].u=edge[num-1].v;58 edge[num].v=edge[num-1].u;59 edge[num].next=head[edge[num].u];60 head[edge[num].u]=num++;61 }62 spfa();63 return 0;64 }