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

Output

Each case takes one line, print the shortest length that XiaoY reach seaside.

Sample Input

5

1 0

1 1

2 0

2 3

3 1

1 1

4 100

0 1

0 1

Sample Output

2

最短路径,用迪杰斯特拉或者SPFA;

#include<iostream>
#include<cstring>
#include<cstdio> using namespace std;
const int N = 10 + 5;
const int INF = (1<<28);
int mat[N][N],D[N];
bool visit[N],near_seaside[N]; void Init(){
for(int i=0;i<N;i++){
for(int j=0;j<N;j++) mat[i][j] = INF;
D[i] = INF,visit[i] = near_seaside[i] = false;
}
}
int solve(const int n){
int cnt,i,k;
D[0] = 0;
for(cnt = 1;cnt<=n;cnt++){
for(k=-1,i=0;i<n;i++)
if(!visit[i]&&(k==-1||D[i] <D[k])) k = i;
for(visit[k]=true,i=0;i<n;i++)
if(!visit[i]&&D[i] > D[k]+mat[k][i])
D[i] = D[k] + mat[k][i];
}
int ans = INF;
for(int i=0;i<n;i++)
if(near_seaside[i]) ans = min(ans,D[i]);
return ans;
} void Input_data(const int n){
int u,v,c;
for(int i=0;i<n;i++){
scanf("%d %d",&u,&c);
if(c) near_seaside[i] = true;
for(int j=0;j<u;j++){
scanf("%d %d",&v,&c);
if(c < mat[i][v]) mat[i][v] = mat[v][i] = c;
}
}
}
int main(){
int n;
while(scanf("%d",&n)==1){
Init();
Input_data(n);
printf("%d\n",solve(n));
}
}

最新文章

  1. 如何在WIN7下进行LINUX虚拟机搭建
  2. js控制html文字提示语的出现和隐藏
  3. 打包并发布自己的Android应用(eclipse)
  4. UITextField和一个UILabel绑定 浅析
  5. 图表工具--- ECharts.js学习(一) 简单入门
  6. webview缓存及跳转时截取url地址、监听页面变化
  7. Java 线程池(ThreadPoolExecutor)原理分析与使用
  8. 【English】九、kids/children/toddlers 三个单词的区别
  9. cmd命令窗口的快速选中复制黏贴
  10. java中的try-catch-finally异常处理(学习笔记)
  11. Windows Community Toolkit 3.0 - CameraPreview
  12. 个人阅读作业WEEK7 (软件工程的瀑布, 大泥球, 教堂,集市,和银弹)
  13. 自学Aruba4.1-Aruba开机初始化
  14. String StringBuffer和StringBuilder区别及性能
  15. LOJ.2863.[IOI2018]组合动作(交互)
  16. C#编程(六十)----------LINQ的概述
  17. array string mysql IN LIKE
  18. 使用 SendARP 获取 MAC 地址(使用SendARP API函数,很多相关文章)
  19. CDN和镜像站点比较
  20. C#设置窗体中的窗体随主窗体大小变化而变化

热门文章

  1. 如何用git将本地代码上传github
  2. MySQL数据库的自动备份与数据库被破坏后的恢复(3)
  3. linux下安装MySQL(mariadb)
  4. win10系统迁移到新的硬盘
  5. LeetCode--064--最小路径和
  6. appium环境666
  7. Shell-04
  8. UOJ37. 【清华集训2014】主旋律
  9. mac 电脑设置密码可以直接使用 passwd 这个命令
  10. Java 线程状态有哪些?