一个人的旅行 HDU - 2066 (最短路)
一个人的旅行
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 50701 Accepted Submission(s): 16857
接着有T行,每行有三个整数a,b,time,表示a,b城市之间的车程是time小时;(1=<(a,b)<=1000;a,b 之间可能有多条路)
接着的第T+1行有S个数,表示和草儿家相连的城市;
接着的第T+2行有D个数,表示草儿想去地方。
Sample Input
6 2 3
1 3 5
1 4 7
2 8 12
3 8 4
4 9 12
9 10 2
1 2
8 9 10
Sample Output
9 思路:最短路的题目,用floyd 会超时的,用dijkstra就可以,只需要把家看作0,并且将相邻的那些火车站的cost边权看为0就好了(cost[0][u]=0)
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<set>
#include<map>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-10
typedef long long ll;
const int maxn = 1e3+;
const int mod = 1e9 + ;
int gcd(int a, int b) {
if (b == ) return a; return gcd(b, a % b);
}
int cost[maxn][maxn];
int d[maxn];
bool used[maxn];
int T,n;
void dijk(int s)
{
fill(d,d+,INF);
fill(used,used+,false);
d[s]=; while(true)
{
int v=-;
for(int u=;u<=n;u++)
{
if(!used[u]&&(v==-||d[u]<d[v]))
v=u;
}
if(v==-)
break;
used[v]=true;
for(int u=;u<=n;u++)
d[u]=min(d[u],d[v]+cost[v][u]);
}
}
int main()
{
int S,D;
while(cin>>T>>S>>D)
{
memset(cost,INF,sizeof(cost));
for(int i=;i<;i++)
cost[i][i]=;
memset(d,INF,sizeof(d));
n=;
while(T--)
{
int a,b,l;
cin>>a>>b>>l;
n=max(max(a,b),n);
if(l<cost[a][b])
{
cost[a][b]=l;
cost[b][a]=l;
}
}
while(S--)
{
int a;
cin>>a;
cost[][a]=;
cost[a][]=;
}
dijk();
int ans=INF;
for(int i=;i<D;i++)
{
int end;
cin>>end;
ans=min(ans,d[end]);
}
cout<<ans<<endl;
}
}
一个人的旅行 HDU - 2066 (最短路)的更多相关文章
- hdu 2066 ( 最短路) Floyd & Dijkstra & Spfa
http://acm.hdu.edu.cn/showproblem.php?pid=2066 今天复习了一下最短路和最小生成树,发现居然闹了个大笑话-----我居然一直写的是Floyd,但我自己一直以 ...
- HDU 2066 最短路floyd算法+优化
http://acm.hdu.edu.cn/showproblem.php?pid=206 题意 从任意一个邻居家出发 到达任意一个终点的 最小距离 解析 求多源最短路 我想到的是Floyd算法 但是 ...
- 一个人的旅行HDU 2066 floyd
一个人的旅行 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- hdu 2066 最短路水题
题意:给出多个可选择的起始点和终点,求最短路 思路:执行起始点次的spfa即可 代码: #include<iostream> #include<cstdio> #include ...
- HDU - 2066 最短路+加一个节点
一个图上,有M条边,Z个出发点,Y个终止点.求一条最短路,其中起点是Z中的任意一点,终点是Y中任意一点. Input 输入数据有多组,输入直到文件结束. 每组的第一行是三个整数M,Z,Y 接着有M行, ...
- # H - H HDU - 2066 (多起点、多终点问题)
H - H HDU - 2066 (多源点.多汇点问题) 一个图上,有M条边,Z个出发点,Y个终止点.求一条最短路,其中起点是Z中的任意一点,终点是Y中任意一点. Input 输入数据有多组,输入直到 ...
- ACM: HDU 2544 最短路-Dijkstra算法
HDU 2544最短路 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descrip ...
- UESTC 30 &&HDU 2544最短路【Floyd求解裸题】
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- hdu 5521 最短路
Meeting Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
随机推荐
- .net 向新页面跳转的语句
1. href='##' onclick=\"window.open('../DataSplit/DrugInfo_ManualVersionViewNew.aspx?id=" + ...
- SpringMVC10 InitBinder 注册自定义编辑器
1.配置web.xml文件 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3// ...
- Java 在使用@Select遇到的问题:拼接字符串将数组拼为了字符串
Java再用@Select拼接sql语句时候, #{参数名}:是加引号的 ${参数名}:是不加引号的 例如: userIds为List或者数组,值为1,2,3,4,5 1.@Select(" ...
- linq 读取xml
xml 文件如下: <?xml version="1.0" encoding="utf-8" ?><nodes> <node> ...
- centos yum 安装mysql5.7 以及 默认root密码查看
1. 首先更新rpm 从MySQL Yum仓库下载最新的rpm文件:http://dev.mysql.com/downloads/repo/yum/ (需要Oracle帐号以及填写一些使用信息,才能 ...
- Flash图表控件FusionCharts调整图表百分比大小
用户可以为图表的宽度和高度设置百分比值,用来替代绝对的像素值. 以百分比的方式调整图表,首先需要更新HTML代码,如下所示: <div id="chartContainer" ...
- fish 与oh-my-fish 的安装
fish 相对于 自带的shell优势很大,最近在研究使用中. 安装教程如下: sudo apt-get install fish oh-my-fish是github上开源项目,使得fish的使用更加 ...
- Log4j知识汇总
Log4j在java开发中还是很常见的,而在日志系统里面也占有举足轻重的地位,想要做好日志相关的工作,了解log4j还是很必要的. 下面就针对 log4j的官方文档user-guide 进行翻译与整理 ...
- w3cschool中jQuery测试结果总结
1.jQuery 是 W3C 标准. 2.$("div#intro .head") 选择器选取:class="intro" 的任何 div 元素中的首个 id= ...
- C++ 关于运算符重载
转载来源:http://c.biancheng.net/cpp/biancheng/view/216.html 重载运算符的函数一般格式如下: 函数类型 operator 运算符名称 (形参表列 ...