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));
}
}

HDU-3665 Seaside的更多相关文章

  1. hdu 3665 Seaside floyd+超级汇点

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3665 题意分析:以0为起点,求到Sea的最短路径. 所以可以N为超级汇点,使用floyd求0到N的最短 ...

  2. HDU 3665 Seaside (最短路,Floyd)

    题意:给定一个图,你家在0,让你找出到沿海的最短路径. 析:由于这个题最多才10个点,那么就可以用Floyd算法,然后再搜一下哪一个是最短的. 代码如下: #pragma comment(linker ...

  3. Seaside HDU 3665 【Dijkstra】

    Problem Description XiaoY is living in a big city, there are N towns in it and some towns near the s ...

  4. hdoj 3665 Seaside【最短路】

    Seaside Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  5. 8-12-COMPETITION

    链接:最短路 A.HDU 2544    最短路 算是最基础的题目了吧.............我采用的是Dijkstra算法....... 代码: #include <iostream> ...

  6. hdu3665Floyd解法

    题目链接:http://icpc.njust.edu.cn/Problem/Hdu/3665/ Floyd是经典的dp算法,将迭代过程分成n个阶段,经过n个阶段的迭代所有点对之间的最短路径都可以求出, ...

  7. HDU 4430 &amp; ZOJ 3665 Yukari&#39;s Birthday(二分法+枚举)

    主题链接: HDU:pid=4430" target="_blank">http://acm.hdu.edu.cn/showproblem.php?pid=4430 ...

  8. HDU 3667.Transportation 最小费用流

    Transportation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  9. HDU 5643 King's Game 打表

    King's Game 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5643 Description In order to remember hi ...

  10. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

随机推荐

  1. flask中自定义日志类

    一:项目架构 二:自定义日志类 1. 建立log.conf的配置文件 log.conf [log] LOG_PATH = /log/ LOG_NAME = info.log 2. 定义日志类 LogC ...

  2. shell之文本过滤(grep)

    shell之文本过滤(grep) 分类: linux shell脚本学习2012-09-14 14:17 588人阅读 评论(0) 收藏 举报 shell正则表达式扩展工具存储 grep(全局正则表达 ...

  3. IDEA unable to find valid certification path to requested target

    一.报错 Could not transfer artifact org.apache.maven.plugins:maven-install-plugin:pom:2.4 from/to alima ...

  4. A1031

    画图,用二维数组作为画布 #include<cstdio> #include<string.h> int main(){ ],u[][]; scanf("%s&quo ...

  5. php日志托管给apache处理

    php.ini配置: log_errors = On;不显示错误display_startup_errors = Offdisplay_errors = Off ;除了notice级别错误外,报告所有 ...

  6. Spring、Hibernate、Struts官方下载地址

    hibernate 官网: http://hibernate.org/ hibernate3 官方下载:http://sourceforge.net/projects/hibernate/files/ ...

  7. Mybatis传多个参数(三种解决方案) mapper.xml的sql语句修改!

    第一种 Public User selectUser(String name,String area); 对应的Mapper.xml <select id="selectUser&qu ...

  8. 《SQL Server 2012 T-SQL基础》读书笔记 - 2.单表查询

    Chapter 2 Single-Table Queries GROUP BY之后的阶段的操作对象就是组(可以把一组想象成很多行组成的)了,HAVING负责过滤掉一些组.分组后的COUNT(*)表示每 ...

  9. Codeforces Gym 100269 Dwarf Tower (最短路)

    题目连接: http://codeforces.com/gym/100269/attachments Description Little Vasya is playing a new game na ...

  10. kvm中添加VNC密码

      #virsh edit 虚机名 <graphics type='vnc' port='5901' autoport='no' listen='0.0.0.0' keymap='en-us'/ ...