Link: http://acm.hdu.edu.cn/showproblem.php? PID = 3665 http://acm.hust.edu.cn/vjudge/contest/view.action? Cid = 29015 # Problem/cseaside
Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 944 accepted submission (s): 670
Problem descriptionxiaoy is living in a big city, there are n towns in it and some towns near the sea. all these towns are numbered from 0 to N-1 and xiaoy lives in the town numbered '0 '. there are some directed roads connecting them. it is guaranteed that you can reach any town
From the town numbered '0', but not all towns connect to each other by roads directly, and there is no ring in this city. one day, xiaoy want to go to the seaside, he asks you to help him find out the shortest way.
Inputthere are several test cases. in each cases the first line contains an integer N (0 <= n <= 10), indicating the number of the towns. then followed n blocks of data, in block-I there are two integers, mi (0 <= mi <= N-1) and PI, then MI lines followed. mi means there
Are mi roads beginning with the I-th Town. pi indicates whether the I-th Town is near to the sea, Pi = 0 means no, Pi = 1 means yes. in next mi lines, each line contains two integers SMI and LMIs, which means that the distance between
I-th Town and the SMI town is LMIS.
Outputeach case takes one line, print the shortest length that xiaoy reach seaside.
Sample Input
51 01 12 02 33 11 14 1000 10 1
Sample output
2
Source2010 Asia Regional Harbin
Question:
Start from a city numbered 0 and find the shortest distance to the coastal city
The first line N indicates the number of cities (number starts from 0)
Then there are n heap data: the first line of heap I indicates that the city numbered I has Mi routes to reach other cities,
Pi indicates whether the city is coastal [0 not coastal, 1 Coastal]
The two numbers in the MI rows below indicate the city number and distance respectively.
Pay attention to the input order and simply process it.
The city number starts from 0.
Algorithm: shortstracode:
# Include <stdio. h> # include <string. h ># include <algorithm> using namespace STD; const int maxn = 20; const int INF = 100000000; int W [20] [20]; int d [maxn]; int vis [maxn]; int s [maxn]; int N; void Dijkstra () {for (INT I = 0; I <n; I ++) d [I] = W [0] [I]; d [0] = 0; memset (VIS, 0, sizeof (VIS); For (INT I = 1; I <= N; I ++) {int M = inf; int X; For (INT y = 0; y <n; y ++) if (! Vis [y] & D [y] <= m) M = d [x = y]; vis [x] = 1; for (INT y = 0; Y <n; y ++) d [y] = min (d [Y], d [x] + W [x] [Y]);} int ans = inf; for (INT I = 0; I <n; I ++) {If (s [I] & D [I] <ans) // find the shortest path of the Coastal region ans = d [I];} printf ("% d \ n", ANS);} int main () {While (scanf ("% d", & N )! = EOF) {for (INT I = 0; I <= N; I ++) for (Int J = 0; j <= N; j ++) W [I] [J] = (I = J? 0: INF); memset (S, 0, sizeof (s); For (INT I = 0; I <n; I ++) {int Mi, PI; scanf ("% d", & Mi, & S [I]); int Si, Li; while (mi --) {scanf ("% d ", & Si, & Li); W [I] [Si] = min (W [I] [Si], Li ); W [Si] [I] = W [I] [Si] ;}} Dijkstra ();} return 0 ;}