Title Link: http://acm.swust.edu.cn/problem/767/
Time limit (ms): $ Memory Limit (KB): 65535 Description
On both sides of the Fujiang River, there are N cities, of which the city on one side belongs to 1 types of city, the other side belongs to 2 types of city, (Special: The city 1 belongs to 1 classes, the City 2 belongs to the 2 category). Now know some of the road conditions, such as Know City 1 to the City 5 has a length of 100 road. The general wants to return from the City 1 to the City 2 home, he began to design the route home. When home due to the relationship between driver's license, can only cross once Fujiang River. Now ask the shortest distance from City 1 to City 2, the road is bidirectional.
Input
Multiple sets of test data.
The first row of each group of data enters the number of cities N (2<=n<=600), and the second line enters the numbers of roads between cities M ((0<=m<=10000),
Next to the M line, enter the information for each road, each row of s,e,t is an integer, representing the city s and city E between the length of a road of T.
The last row n can only be 1 or 2 for which class I cities belong.
In order to simplify the topic, we always think that the city 1 belongs to 1 classes, the City 2 belongs to the 2 class N = 0 When the end is expressed
Output
Output City 1 to the shortest path of City 2, output-1 if not present
Sample Input
| 211 2 1001 2331 2 1001 3 402 3 501 2 1553 1 2005 3 1502 5 1604 3 1704 2 1701 2 2 2 10 |
Sample Output
Discussion layouts
Problem-solving ideas: A Dijkstra problem, pay attention to a class of cities, two cities belong to the river on both sides, and the general can only cross the river once the optimization path is to pay attention to the OK
!VIS[J] && (v[sign] = = 1 | | (V[sign] = = 2 && v[j] = = 2)) && Dis[j] > Dis[sign] + mpt[sign][j]
Only once across the river if you change the one or two-city connectivity state to one-way it should also be possible to try Orz~~~
The code is as follows:
1#include <stdio.h>2#include <string.h>3 #defineINF 0x3f3f3f3f4 5 intM, N, mpt[605][605], v[605], dis[605], vis[605];6 voidDijkstra () {7 intI, J, Minn, sign;8 for(i =2; I <= N; i++) Dis[i] = mpt[1][i];9vis[1] =1;Tendis[1] =0; One for(i =2; I <= N; i++){ AMinn =inf; - for(j =2; J <= N; J + +){ - if(!VIS[J] && minn >Dis[j]) { theMinn =Dis[j]; -Sign =J; - } - } + if(Minn >= INF) Break; -Vis[sign] =1; + for(j =2; J <= N; J + +){ A if(!vis[j] && (v[sign] = =1|| (V[sign] = =2&& V[j] = =2)) && Dis[j] > Dis[sign] +Mpt[sign][j]) atDIS[J] = Dis[sign] +Mpt[sign][j]; - } - } - if(dis[2] >=inf) -printf"-1\n"); - Else inprintf"%d\n", dis[2]); - } to + intMain () { - intI, A, B, C; the while(~SCANF ("%d", &n) &&N) { *scanf"%d", &m); $memset (MPT, INF,sizeof(MPT));Panax Notoginsengmemset (Vis,0,sizeof(Vis)); - for(i =1; I <= m; i++){ thescanf"%d%d%d", &a, &b, &c); + if(c >= Mpt[a][b])Continue; AMPT[A][B] =C; theMpt[b][a] =C; + } - for(i =1; I <= N; i++) scanf ("%d", &v[i]); $ Dijkstra (); $ } - return 0; -}View Code
[Swust OJ 767]--Home (Dijkstra algorithm)