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. [每日一讲] Python系列:字符串(上)

    字符串作为人类最常处理的内容,在计算中决定了其占有重要的地位.在 Python 中,字符串的操作和处理往往需要根据实际问题,结合其他操作才可以完成目标.在复杂世界仅仅是字符串 API 还无法完成工作. ...

  2. React native 之 Promise

    关键词:Promise Promise.all Promise是什么?=> https://www.runoob.com/w3cnote/es6-promise.html Promise.all ...

  3. 如何在MySQL中使用explain查询SQL的执行计划?

    1.什么是MySQL执行计划 要对执行计划有个比较好的理解,需要先对MySQL的基础结构及查询基本原理有简单的了解. MySQL本身的功能架构分为三个部分,分别是 应用层.逻辑层.物理层,不只是MyS ...

  4. 批量下载文件web

    最近需要这个所以写了一个例子一般批量下载由以下步骤组成: 1.确定下载的源文件位置 2.对文件进行打包成临时文件,这里会用到递归调用,需要的嵌套的文件夹进行处理,并返回文件保存位置 3.将打包好的文件 ...

  5. Java——面向对象编程

    在面向对象的编程中,不能再有第一步.第二步怎么做的概念.   [对象和类]  

  6. [USACO2019JAN]Sleepy Cow Sorting题解

    拿到这个问题,我们从头开始思考. 我们把序列看做两部分,一部分在前表示待排序的,记为序列1,一部分在后表示已排序的,记为序列2. 因为序列2在后,所以不必担心它影响序列1的排序,那么对于序列1的第一个 ...

  7. 双系统使用Linux引导

    今天在装linux的window双系统时,出现在无法使用linux引导的问题,开机总是自动进windows,照理来说我先装的window,后装的linux,应该是开机进grub引导才对.在主板的boo ...

  8. 3D Computer Grapihcs Using OpenGL - 11 Model View Projection Matrices

    本节我们将绘制一个3维物体,立方体. 如果要渲染3D物体,我们需要了解MVP(Model View Projection),它表示三个转换矩阵.实际上这个名字不够明确,更加确切的释义如下: Model ...

  9. Java 有关类字段的初始化

    实例代码 package text; public class MethodOverload { /** * @param args */ public static void main(String ...

  10. Java网络编程与NIO详解11:Tomcat中的Connector源码分析(NIO)

    本文转载 https://www.javadoop.com 本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https://github.c ...