Seaside

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1356    Accepted Submission(s):
973

Problem 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 LMi, which means that the distance
between the i-th town and the SMi town is LMi.
 
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
 
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define INF 0x3f3f3f
#define MAX 110
#define minn(x,y)(x<y?x:y)
using namespace std;
int map[MAX][MAX];
int vis[MAX],used[MAX],low[MAX];
int n,m;
void init()
{
int i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
map[i][j]=i==j?0:INF;
}
void solve()
{
int i,j,min,next,mindis=0;
memset(vis,0,sizeof(vis));
for(i=0;i<n;i++)
low[i]=map[0][i];
vis[0]=1;
for(i=1;i<n;i++)
{
min=INF;
for(j=0;j<n;j++)
{
if(!vis[j]&&min>low[j])
{
next=j;
min=low[j];
}
}
vis[next]=1;
for(j=0;j<n;j++)
{
if(!vis[j]&&low[j]>map[next][j]+low[next])
low[j]=map[next][j]+low[next];
}
}
int sum=INF;
for(i=0;i<n;i++)
{
if(used[i]==1)
sum=minn(sum,low[i]);
}
printf("%d\n",sum);
}
int main()
{
int i,j;
while(scanf("%d",&n)!=EOF)
{
init();
int a,b,c;
int road,sea;
memset(used,0,sizeof(used));
int ok=0;
for(i=0;i<n;i++)
{
scanf("%d%d",&road,&sea);
if(sea==1)
used[i]=1;
while(road--)
{
scanf("%d%d",&a,&b);
if(map[i][a]>b)
map[i][a]=b;
}
}
if(used[0]==1)
{
printf("0\n");
ok=1;
continue;
}
if(!ok)
solve();
}
return 0;
}

  

hdoj 3665 Seaside【最短路】的更多相关文章

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

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

  2. hdu 3665 Seaside floyd+超级汇点

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

  3. 【HDOJ】2544 最短路

    Dijkstra. #include <stdio.h> #include <string.h> #define INF 0xfffffff ][]; ]; ]; int ma ...

  4. HDOJ/HDU 2544 最短路---dijkstra算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 这题的思路可以见这里(同一类型):http://blog.csdn.net/xiaozhuaix ...

  5. 8-12-COMPETITION

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

  6. hdoj 2066 一个人的旅行 【多源多汇最短路】

    题目:hdoj 2066 一个人的旅行 方法:缩点 + 最短路 分析:看了大神的一篇博客,讲冗余压缩的,然后就想找一个多源最短路练练手. 这个题目就是典型的多源多汇最短路 方法:把全部的源点压缩成一个 ...

  7. hdoj 2544 最短路【dijkstra or spfa】

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  8. hdoj 1874 畅通工程续(单源最短路+dijkstra)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 思路分析:该问题给定一个无向图.起始点和终点,要求求出从起始点到终点的最短距离: 使用Dijks ...

  9. HDOJ 1217 Arbitrage (最短路)

    题意:每两种货币之间都有不同的汇率  如果换回自己最后是赚的 输出Yes 否则是No 因为最多只有三十种货币 所以用Floyd是可行的 与一般的最短路板子不同的地方 汇率是要乘而不是加 如果乘上一个小 ...

随机推荐

  1. OpenCV2学习笔记01:Linux下OpenCV开发环境的搭建

    个人已经厌倦了Windows下的开发方式,于是决定转到Linux平台上来,当然我也知道这个转变会很艰辛,但是我还是要坚持.所以,后面的所有开发我都会基于Linux和Qt,先从开发环境的搭建开始做起,当 ...

  2. js 的基础知识变量

    什么是变量? 变是一个存储和释放我的数据! 我们用var关键字来声名变量,声名多个变量时用逗号来隔开 在变量没有赋值之前,显示是一个未定义的变量! <script> var a; var ...

  3. Mock相关收集

    MockMVC+Mockito http://www.cnblogs.com/syxchina/p/4150879.html Spring中使用Mockito http://www.cnblogs.c ...

  4. echo、print、print_r、printf、sprintf、var_dump的区别比较

    一.echoecho() 实际上不是一个函数,是php语句,因此您无需对其使用括号.不过,如果您希望向 echo() 传递一个以上的参数,那么使用括号会发生解析错误.而且echo是返回void的,并不 ...

  5. mongodb3.2系统性学习——2、write concern mongodb 写安全机制

    为了尊重作者原文章位置:http://kyfxbl.iteye.com/blog/1952941 首先讲一下mongodb 的写操作过程: mongodb有一个write concern的设置,作用是 ...

  6. mongodb篇二:mongodb克隆远程数据库,去重查询的命令及对应java语句

    http://blog.csdn.net/qkxh320/article/details/16115671 1.首先操作mongodb最基本命令:: show databases;           ...

  7. MDK常用快捷键

    一.常用编译相关的快捷键 1.编译(单个文件)  Ctrl+F7 2.连接 F7 二.常用调试相关的快捷键 1.运行/停止     Ctrl+F5 2.Run(全速运行)  F5 3.Stop Deb ...

  8. java 集合(二)

    1.练习题 如果输入的字符里有非英语字母的,不给于执行

  9. spring 下载地址(拷贝)

    Spring2.5.6 和Spring3.0.5所有jar下载地址spring jar包 官方下载地址 文档下载地址.2.56版本 和3.05版本http://s3.amazonaws.com/dis ...

  10. Codeforces Round #197 (Div. 2) : D

    这题也是一个线段树的水题: 不过开始题目没看明白,害得我敲了一个好复杂的程序.蛋疼啊.... 最后十几分钟的时候突然领悟到了题意,但是还是漏掉一个细节,老是过不去... 以后比赛的时候不喝啤酒了,再也 ...