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. is_numeric漏洞分析

    2014年,PHP中is_numeric函数十六进制绕过漏洞引发了一次安全问题 接下来,我们来分析一下这个漏洞的原理 函数介绍: is_numeric — 检测变量是否为数字或数字字符串. 函数原型: ...

  2. SpringMVC项目学习1_web.xml

    最近接触的所有项目都是SpringMVC+ajax的项目,因此以一个项目为例学习下. --------------------------------------------------------- ...

  3. 使用JAXB来实现Java合xml之间的转换

    使用jaxb操作Java与xml之间的转换非常简单,看个例子就明白了. //javaBean-->xml @Test public void test1() { try { JAXBContex ...

  4. sql if

    SELECT a.id, a.EduSiteNo, a.EduSiteName, a.SchoolId, a.LinkMan, a.Tel, a.Mobile, a.Fax, a.Address, C ...

  5. ECshop--导航栏模块细究

    花了一下午的时间,总算解决了. 本来想在前台界面上分析看看ecshop导航栏设置,在浏览器里面是定位到了"nav-manu"下面,然后子目录是在m_left下可以看到是动态生成一系 ...

  6. Android Studio 2.2 HTTP proxy setting 提示异常

    操作系统 :MacOS 10.11.6 IDE :Android Studio 2.2 Java Version :1.8 异常现象描述: 在给Android Studio 2.2设置代理时,出现警告 ...

  7. IXListView的自我分析一

    XListView是一个很不错的用来刷新和加载的控件,下拉刷新和上拉加载.目前这个控件已经没有更新,这个不重要,重要的是它确实还不错,之后可能一直有人在用. android没有提供原生的这类控件,需要 ...

  8. Ubuntu常用终端快捷键

    CTRL+k:删除从光标到行尾的部分 CTRL+u:删除从光标到行首的部分 ALT+d:删除从光标到当前单词结尾的部分 CTRL+w:删除从光标到当前单词开头的部分 CTRL+a:将光标移到行首 CT ...

  9. Lucene 排序 Sort与SortField

    在sql语句中,有升序和降序排列.在Lucene中,同样也有. Sort里的属性 SortField里的属性 含义 Sort.INDEXORDER SortField.FIELD_DOC 按照索引的顺 ...

  10. mysql5.5提示Deprecated: mysql_query(): The mysql extension is deprecated

    解决方法1:在php程序代码里面设置报警级别 <?php error_reporting = E_ALL & ~E_DEPRECATED 方法2:禁止php报错 display_erro ...