Choose the best route
hdu 2680:http://acm.hdu.edu.cn/showproblem.php?pid=2680
在图论中注意重边问题是必须的,有向无向也是同等重要的,如这道题 from station p to station q说的就很清楚是有向图
此题如果暴力求解把每个临近的车站都作为源点走一遍,就会超时。此时的做法是
在与临近的车站加上一个0,并使其的距离为零,这样就可以转化成单源点的问题
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxint=;
int dist[maxint];
int c[maxint][maxint];
int s[maxint];
int qu;
int n,m;
void solve(int v){
for(int i=;i<=n;i++){
dist[i]=c[v][i];
s[i]=;
}
s[v]=;
dist[v]=;
for(int i=;i<=n;i++){
int min=;
int k;
for(int j=;j<=n;j++){
if(!s[j]&&dist[j]<min) { min=dist[j];
k=j; }
}
if(min==)
break;
s[k]=;
for(int j=;j<=n;j++)
{
if(s[j]==&&dist[j]>dist[k]+c[k][j])
dist[j]=dist[k]+c[k][j];
} }
}
int main(){
while(scanf("%d %d %d",&n,&m,&qu)!=EOF){
for(int i=;i<;i++){
for(int j=;j<;j++){
c[i][j]=;
}
}
int a,b,w;
for(int i=;i<=m;i++){
scanf("%d%d%d",&a,&b,&w);
if(w<c[a][b]){
c[a][b]=w;
}
}//无向图,并且去重
int e;
scanf("%d",&e);
int f;
for(int i=;i<e;i++){
scanf("%d",&f);
c[][f]=;
}
solve();
if(dist[qu]==)
printf("-1\n");
else
printf("%d\n",dist[qu]); } }
Choose the best route的更多相关文章
- hdu 2680 Choose the best route
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2680 Choose the best route Description One day , Kiki ...
- HDU2680 Choose the best route 最短路 分类: ACM 2015-03-18 23:30 37人阅读 评论(0) 收藏
Choose the best route Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- Choose the best route(最短路)dijk
http://acm.hdu.edu.cn/showproblem.php?pid=2680 Choose the best route Time Limit: 2000/1000 MS (Java/ ...
- HDU2680 Choose the best route 2017-04-12 18:47 28人阅读 评论(0) 收藏
Choose the best route Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Othe ...
- hdu 2680 Choose the best route (dijkstra算法 最短路问题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2680 Choose the best route Time Limit: 2000/1000 MS ( ...
- hdu-2680 Choose the best route(最短路)
题目链接: Choose the best route Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- 最短路问题-- Dijkstra Choose the best route
Choose the best route Problem Description One day , Kiki wants to visit one of her friends. As she i ...
- hdoj 2680 choose the best route
Problem Description One day , Kiki wants to visit one of her friends. As she is liable to carsicknes ...
- HDU 2680 Choose the best route(SPFA)
Problem DescriptionOne day , Kiki wants to visit one of her friends. As she is liable to carsickness ...
- HDU 2068 Choose the best route
http://acm.hdu.edu.cn/showproblem.php?pid=2680 Problem Description One day , Kiki wants to visit one ...
随机推荐
- 插入ts以及判断列是否存在(支持多数据库)
1:增加ts.dr字段,先判断ts.dr字段是否存在,其中ts字段插入的是日期,默认值为当前插入的时间,dr字段是数值型,默认值为0 * 增加ts/dr字段 * * @param tableList ...
- cocos2dx 文件处理
问题1:fopen 在vs下使用fopen进行文件处理,跑通了,但是移植到android源码下时就出现了一大推问题,首先需要理解的是在vs下开发资源是存放在执行文件的相同目录下的,而移植到androi ...
- CentOS 6.7平台nginx压力测试(ab/webbench)
压力测试工具一:webbench 1.安装 wget http://home.tiscali.cz/~cz210552/distfiles/webbench-1.5.tar.gz tar zxvf w ...
- JVM Davilk ART 简介 区别
JVM和DVM JavaSE 程序使用的虚拟机叫 Java Virtual Machine,简称 JVM.Android 平台虽然是使用java语言来开发应用程序,但Android程序却不是运行在标准 ...
- Lucene.net项目研究说明
最近项目需要全文检索,所以找了几个开源的.NET检索项目,如:Lucene.net,Sphinx,Hubble.net.最后选择使用Lucene.ne来实现全文检索.至于原因嘛,可以参考下面几点: 1 ...
- 一个误解: 单个服务器程序可承受最大连接数“理论”上是“65535”
转载:http://www.cnblogs.com/tianzhiliang/archive/2011/06/13/2079564.html 请注意,这里有两个词分别被我标记上了引号,一个是" ...
- MSSQL生成整个数据库的SQL脚本的工具 scptxfr.exe
scptxfr.exe的路径要正确declare @cMd varchar(1000)set @cmd = 'master.dbo.xp_cmdshell ' + '''c:\"Micros ...
- Javascript字符串拼接小技巧
在Javascript中经常会遇到字符串的问题,但是如果要拼接的字符串过长就比较麻烦了. 如果是在一行的,可读性差不说,如果要换行的,会直接报错. 在此介绍几种Javascript拼接字符串的技巧. ...
- 菜鸟日记之 java中的集合框架
java中的集合框架图 如图所示:java中的集合分为两种Collection和Map两种接口 可分为Collection是单列集合和Map的双列集合 Collection单列集合:继承了Iterat ...
- 阻塞式和非阻塞式IO
有很多人把阻塞认为是同步,把非阻塞认为是异步:个人认为这样是不准确的,当然从思想上可以这样类比,但方式是完全不同的,下面说说在JAVA里面阻塞IO和非阻塞IO的区别 在JDK1.4中引入了一个NIO的 ...