hdoj 3665 Seaside【最短路】
Seaside
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1356 Accepted Submission(s):
973
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.
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.
that XiaoY reach seaside.
#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【最短路】的更多相关文章
- HDU 3665 Seaside (最短路,Floyd)
题意:给定一个图,你家在0,让你找出到沿海的最短路径. 析:由于这个题最多才10个点,那么就可以用Floyd算法,然后再搜一下哪一个是最短的. 代码如下: #pragma comment(linker ...
- hdu 3665 Seaside floyd+超级汇点
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3665 题意分析:以0为起点,求到Sea的最短路径. 所以可以N为超级汇点,使用floyd求0到N的最短 ...
- 【HDOJ】2544 最短路
Dijkstra. #include <stdio.h> #include <string.h> #define INF 0xfffffff ][]; ]; ]; int ma ...
- HDOJ/HDU 2544 最短路---dijkstra算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 这题的思路可以见这里(同一类型):http://blog.csdn.net/xiaozhuaix ...
- 8-12-COMPETITION
链接:最短路 A.HDU 2544 最短路 算是最基础的题目了吧.............我采用的是Dijkstra算法....... 代码: #include <iostream> ...
- hdoj 2066 一个人的旅行 【多源多汇最短路】
题目:hdoj 2066 一个人的旅行 方法:缩点 + 最短路 分析:看了大神的一篇博客,讲冗余压缩的,然后就想找一个多源最短路练练手. 这个题目就是典型的多源多汇最短路 方法:把全部的源点压缩成一个 ...
- hdoj 2544 最短路【dijkstra or spfa】
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- hdoj 1874 畅通工程续(单源最短路+dijkstra)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 思路分析:该问题给定一个无向图.起始点和终点,要求求出从起始点到终点的最短距离: 使用Dijks ...
- HDOJ 1217 Arbitrage (最短路)
题意:每两种货币之间都有不同的汇率 如果换回自己最后是赚的 输出Yes 否则是No 因为最多只有三十种货币 所以用Floyd是可行的 与一般的最短路板子不同的地方 汇率是要乘而不是加 如果乘上一个小 ...
随机推荐
- struts2与spring集成时action的class属性值意义
struts2单独使用时action由struts2自己负责创建:与spring集成时,action实例由spring负责创建(依赖注入).这导致在两种情况下struts.xml配置文件的略微差异. ...
- cenots 下的 lamp(备份与恢复)
用 putty连接数据库: mysql -uroot -p密码 create database yourdb DEFAULT CHARACTER SET utf8 COLLATE utf8_chine ...
- Delphi 我常用的几个下载源码的站点
盒子.Delphi园地就不说了,介绍几个其它的: 源码爱好者,特别喜欢. http://www.codefans.net/sort/list_10_1.shtml 新兴源码: http://www.n ...
- 【C语言】中的布尔类型
C语言中的布尔类型 一.相关基础知识 首先bool true false为C++中的关键字,C语言中默认不支持这几个字符! 二.具体内容 在C89 (ANSI C)标准中没有定义与布尔类型相关的内 ...
- shell编程的一些例子3
数值处理 1.let 命令 bash 的内部命令let可以用来计算算术表达式的值.如果表达式中有空格或者特殊字符,则应将表达式括在双引号中. let的语法命令:let express-list 如果最 ...
- Java笔记2 : 泛型的体现,及其上限、下限、通配符
Java中的泛型是在jdk5.0引入的,语法不难,但是需要注意的细节有很多,这里写一下备忘. 首先是最简单的泛型类,泛型方法,泛型接口: //泛型接口的定义 interface MyInter< ...
- linux vi 使用
vi 有一般模式和编辑模式 如vi test.txt 是首先进入的一般模式,一般模式下只能进行复制.删除.粘贴文件数据, 在一般模式下按i .I.a.A.o.O 都能进入编辑模式,按下不同的键进入编辑 ...
- 【技术帖】解决 Hudson jenkins 连接等待中 - Waiting for next av
今天构建项目发现如下问题: jenkins 连接等待中 - Waiting for next available executor 左下角那块一直不运行构建,一直在连接等待. 于是,进入一级页面, 右 ...
- 四校训练 warm up 14
A:Pythagoras's Revenge 代码: #include<cstdio> #define ll long long using namespace std; int main ...
- [wikioi]拦截导弹
http://wikioi.com/problem/1044/ 这道题是DP.前一问很自然可以规约成最长不升(含等号下降)子序列.难点在后一问为何能规约成最长上升子序列.后来看了网上的回答,仍然没有简 ...