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. 详解Spring MVC 集成EHCache缓存_java - JAVA

    文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 废话少说,直接上代码: ehcache.xml 文件 <?xml version="1.0" ...

  2. 《SaltStack技术入门与实践》——执行结果处理

    执行结果处理 本章节参考<SaltStack技术入门与实践>,感谢该书作者: 刘继伟.沈灿.赵舜东 Return组件可以理解为SaltStack系统对执行Minion返回后的数据进行存储或 ...

  3. 【GDOI 2016 Day1】第二题 最长公共子串

    分析 首先,可以发现,区间是可以合并滴.把区间按左端点排序,对于两个区间[l1,r1].[l2,r2],当l1<=l2 and r1>=l2,那么,将它们合成一个新的区间[l1,r2].当 ...

  4. React Native 之react-native-sqlite-storage

    npm 官网指导: https://www.npmjs.com/package/react-native-sqlite-storage 1. 执行: npm install react-native- ...

  5. HDU 4027 Can you answer these queries? (线段树成段更新 && 开根操作 && 规律)

    题意 : 给你N个数以及M个操作,操作分两类,第一种输入 "0 l r" 表示将区间[l,r]里的每个数都开根号.第二种输入"1 l r",表示查询区间[l,r ...

  6. HDU 2296 Ring ( Trie图 && DP && DP状态记录)

    题意 : 给出 m 个单词,每一个单词有一个权重,如果一个字符串包含了这些单词,那么意味着这个字符串拥有了其权重,问你构成长度为 n 且权重最大的字符串是什么 ( 若有权重相同的,则输出最短且字典序最 ...

  7. Laya 首日红点逻辑

    Laya 首日红点逻辑 @author ixenos 2019-08-26 10:50:27 1.原理:显然,首日红点意味着包含进程销毁的情况,那么就要持久化存储信息,这里我们使用LocalStora ...

  8. you have not created a boot efi partition

    rhel6.8 自定义分区,创建 /boot/efi 分区找不到”EFI System Partition“文件系统, 选其他文件系统 next时都提示:you have not created a ...

  9. (转)sqlite developer注册方法

    本文转载自:http://blog.csdn.net/fm0517/article/details/7912525 删除注册表中HKEY_CURRENT_USER\SharpPlus\SqliteDe ...

  10. linux管理权限

    1.linux命令查询 root id 2.切换用户 su - xiaobai 一定要加"  -  "这个会将你的所有环境变量都带过来 3.root用户切换普通用户不需要输入密码反 ...