从起点0开始,到达最近的那个是海边的城镇的距离..
思路:
水的最短路,随你怎么写,dij,floyd,spfa..都行,只要你喜欢..我写的spfa好久不写了,复习下.
#include<stdio.h> #include<queue> #define N_node 10 + 5 #define N_edge 200 + 10 #define inf 1000000000 using namespace std; typedef struct { int to ,next ,cost; }STAR; STAR E[N_edge]; int list[N_node] ,tot; int s_x[N_node]; void add(int a ,int b ,int c) { E[++tot].to = b; E[tot].cost = c; E[tot].next = list[a]; list[a] = tot; } void spfa(int s ,int n) { for(int i = 0 ;i <= n ;i ++) s_x[i] = inf; s_x[s] = 0; int mark[N_node] = {0}; mark[s] = 1; queue<int>q; q.push(s); while(!q.empty()) { int xin ,tou; tou = q.front(); q.pop(); mark[tou] = 0; for(int k = list[tou] ;k ;k = E[k].next) { xin = E[k].to; if(s_x[xin] > s_x[tou] + E[k].cost) { s_x[xin] = s_x[tou] + E[k].cost; if(!mark[xin]) { mark[xin] = 1; q.push(xin); } } } } return ; } int main () { int mk_hai[N_node]; int i ,j ,n ,m ,p ,b ,c; while(~scanf("%d" ,&n)) { memset(mk_hai ,0 ,sizeof(mk_hai)); memset(list ,0 ,sizeof(list)); tot = 1; for(i = 1 ;i <= n ;i ++) { scanf("%d %d" ,&m ,&p); if(p)mk_hai[i] = 1; for(j = 1 ;j <= m ;j ++) { scanf("%d %d" ,&b ,&c); add(i ,b + 1 ,c); add(b + 1 ,i ,c); } } spfa(1 ,n); int ans = inf; for(i = 1 ;i <= n ;i ++) if(mk_hai[i] && ans > s_x[i]) ans = s_x[i]; printf("%d\n" ,ans); } return 0; }