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. php中getimagesize函数的用法

    php获取图片信息getimagesize,php自带函数.获取图片的类型,尺寸的方法有许多,该函数仅是方法之一. getimagesize() 函数将测定任何 GIF,JPG,PNG,SWF,SWC ...

  2. #Leet Code# Evaluate Reverse Polish Notation

    描述:计算逆波兰表达法的结果 Sample: [", "*"] -> ((2 + 1) * 3) -> 9 [", "/", & ...

  3. AppStore IPv6-only审核被拒原因分析及解决方案-b

    自2016年6月1日起,苹果要求所有提交App Store的iOS应用必须支持IPv6-only环境,背景也是众所周知的,IPv4地址已基本分配完毕,同时IPv6比IPv4也更加高效,向IPv6过渡是 ...

  4. Mac下获取AppStore安装包文件路径

    通过远在大洋彼岸的苹果服务器下载东西,确实有够慢啊!AppStore更甚:甚至都经常提示连不上服务器,而有些软件呢,还必须从AppStore下载安装,所以没办法,谁让上了苹果的贼船呢!公司的网速更是不 ...

  5. DM8168 debug continue... ...

    1.boot   VFS: Unable to mount root fs via NFS, trying floppy.   VFS: Cannot open root device "n ...

  6. JNI/NDK开发指南(二)——JVM查找java native方法的规则

    通过第一篇文章,大家明白了调用native方法之前,首先要调用System.loadLibrary接口加载一个实现了native方法的动态库才能正常访问,否则就会抛出java.lang.Unsatis ...

  7. 重燃你的PHP安全分析之火

    关于脚本安全这个话题好像永远没完没了,如果你经常到国外的各种各样的bugtraq上,你会发现有一半以上都和脚本相关,诸如SQL injection,XSS,Path Disclosure,Remote ...

  8. PHP传引用报错(5.4版本)

    php5.3系列版本以及以前版本,传引用没有什么问题,升级到php5.4以后,传引用的地方,全报错 Fatal error: Call-time pass-by-reference has been ...

  9. 【HDU 5233】Tree chain problem (树形DP+树剖+线段树|树状数组)最大权不相交树链集

    [题目] Tree chain problem Problem Description Coco has a tree, whose vertices are conveniently labeled ...

  10. 记录一下MYSQL的SQL语法

    这是加时间的语法 update  mylog set  mydate= DATE_ADD( mydate, INTERVAL 13 HOUR) WHERE mydate BETWEEN '2014-0 ...