HDU 3665 Seaside

来源:互联网 发布:矩阵和伴随矩阵的秩 编辑:程序博客网 时间:2024/05/20 17:27

Seaside

Time Limit:1000MS    Memory Limit:32768KB    64bit IO Format:%I64d & %I64u

Description

XiaoY 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.

Input

There 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 L Mi, which means that the distance between the i-th town and the SMi town is L Mi.

Output

Each 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
#include<stdio.h>#include<string.h>#define INF 0x3f3f3f#include<algorithm>using namespace std;int map[15][15];int dis[15];int n;int floyd(){for(int k=1;k<n;k++)for(int i=0;i<n;i++)for(int j=0;j<n;j++){if(map[i][j]>map[i][k]+map[k][j])map[i][j]=map[i][k]+map[k][j];}}int main(){int a,b,c,d,res;while(~scanf("%d",&n)){for(int i=0;i<n;i++){for(int j=0;j<n;j++)map[i][j]=INF;map[i][i]=0;}memset(dis,0,sizeof(dis));res=INF;for(int i=0;i<n;i++){scanf("%d%d",&a,&b);dis[i]=b;for(int j=0;j<a;j++){scanf("%d%d",&c,&d);map[i][c]=d;}}floyd();for(int i=0;i<n;i++){if(dis[i] && map[0][i]<res)res=map[0][i];}printf("%d\n",res);}}


0 0