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是可行的 与一般的最短路板子不同的地方 汇率是要乘而不是加 如果乘上一个小 ...
随机推荐
- div 被Object盖住的。解决办法
今天遇到一个比较头疼的问题,就是在一个标签上右键,弹出的菜单div被标签内的Office控件Object挡住了下半部分,始终无法显示.查了好多解决方案,最终都不能解决问题,几乎都要放弃了.中午吃饭的时 ...
- hbuilder工具快捷键 http://www.qq210.com/shoutu/android
http://www.qq210.com/shoutu/android 创建HTML结构: h 8 (敲h激活代码块列表,按8选择第8个项目,即HTML代码块,或者敲h t Enter)中途换行: ' ...
- PHP 易出问题记录
PHP foreach引用缺陷 <?php $array = array(1, 2, 3); foreach ($array as &$v) {} foreach ($array as ...
- mysql统计表的大小
如下是sql语句: SELECT TABLE_NAME as name,DATA_LENGTH+INDEX_LENGTH as len,TABLE_ROWS as rows FROM informat ...
- REST API之前端跨域访问
关键字:跨域访问,cross-origin, NodeJS, REST API, JavaScript, Access-Control-Allow-Origin 1.新建并运行一个 NodeJS的se ...
- ConfigParser---python
# !/usr/bin/python # Filename:tcfg.py import ConfigParser config = ConfigParser.ConfigParser() confi ...
- Django web 开发指南 no such table:
在学习django web开发指南时,发布新博客点击save后会有error提示:no such table balabalabala... 百度了一下说重新运行manage.py syncdb 就可 ...
- lucene解决全文检索word2003,word2007的办法
在上一篇文章中 ,lucene只能全文检索word2003,无法检索2007,并且只能加载部分内容,无法加载全文内容.为解决此问题,找到了如下方法 POI 读取word (word 2003 和 wo ...
- 从IT的角度思考BIM(二):模式与框架
我们满怀着美好期许,鼓起勇气敲响了 BIM 世界的大门.忽然人群中有人高呼:BIM 已死,大家都散了吧! 这时人群开始骚动起来.“我早就说这玩意是忽悠人的吧,你们不信还偏要来”,“我花了好多钱准备这次 ...
- MATLAB conv2卷积的实现
MATLAB conv2卷积的实现 二维卷积的算法原理比较简单,参考任意一本数字信号处理的书籍,而matlab的conv2函数的滤波有个形状参数,用下面的一张图很能说明问题: 这里给出一种最原始的实现 ...