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的更多相关文章

  1. hdu 2680 Choose the best route

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2680 Choose the best route Description One day , Kiki ...

  2. 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 ...

  3. Choose the best route(最短路)dijk

    http://acm.hdu.edu.cn/showproblem.php?pid=2680 Choose the best route Time Limit: 2000/1000 MS (Java/ ...

  4. 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 ...

  5. 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 ( ...

  6. hdu-2680 Choose the best route(最短路)

    题目链接: Choose the best route Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K ( ...

  7. 最短路问题-- Dijkstra Choose the best route

    Choose the best route Problem Description One day , Kiki wants to visit one of her friends. As she i ...

  8. hdoj 2680 choose the best route

    Problem Description One day , Kiki wants to visit one of her friends. As she is liable to carsicknes ...

  9. HDU 2680 Choose the best route(SPFA)

    Problem DescriptionOne day , Kiki wants to visit one of her friends. As she is liable to carsickness ...

  10. HDU 2068 Choose the best route

    http://acm.hdu.edu.cn/showproblem.php?pid=2680 Problem Description One day , Kiki wants to visit one ...

随机推荐

  1. Linux Kernel 排程機制介紹

    http://loda.hala01.com/2011/12/linux-kernel-%E6%8E%92%E7%A8%8B%E6%A9%9F%E5%88%B6%E4%BB%8B%E7%B4%B9/ ...

  2. 插入ts以及判断列是否存在(支持多数据库)

    1:增加ts.dr字段,先判断ts.dr字段是否存在,其中ts字段插入的是日期,默认值为当前插入的时间,dr字段是数值型,默认值为0 * 增加ts/dr字段 * * @param tableList ...

  3. 对于Maven管理的项目制定虚拟目录

    基于Maven管理的web项目结构: target目录是用来存放项目打包之后生成的文件的目录,此目录中的文件必须调用mvn clean package后才能生成, 如果把虚拟目录设置在此目录中,则每次 ...

  4. xshell连接本地Linux虚拟机!

    终端输入ifconfig获取本地虚拟机的IP地址; 安装openssh-server sudo apt-get install openssh-server 查看server是否启动: ps -ef ...

  5. System Operations on AWS - Lab 5W - Managing Resources (Windows)

    登陆到CommandHost实例,通过AWS CLI来管理各个资源 1. 通过Tags来管理资源 1.1 登陆到CommandHost实例 1.2 找出development实例 1.2.1 打开Po ...

  6. apache日志介绍

    apache日志介绍:    通用日志格式: CommonLogFormat    组合日志格式: CombinedLogFormat    例如:        <VirtualHost *: ...

  7. C++ 常见问题

    1:保证编译后方法名不被修改:  The: extern "C" { function declarations here in h file } will disable C++ ...

  8. o] TortoiseGit错误 - Could not get all refs. libgit2 returned: corrupted loose reference file

    因无法追溯的同步操作错误或工程文件错误,造成Git 同步时报错: Could not get all refs. libgit2 returned: corrupted loose reference ...

  9. SQL Server 中WITH (NOLOCK)

    with(nolock)的功能: 1: 指定允许脏读.不发布共享锁来阻止其他事务修改当前事务读取的数据,其他事务设置的排他锁不会阻碍当前事务读取锁定数据.允许脏读可能产生较多的并发操作,但其代价是读取 ...

  10. java 反射 - 获取成员变量的值.

    通过反射,可以获取所有声明的成员变量(包括所有的),代码如下: package spt.test.src; public class Person { private String name = &q ...