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 ...
随机推荐
- LabVIEW系列——合并错误(VI)的用法
Merge Errors.vi的功能:1.按顺序搜索错误输入1,2,3,以及错误数组输入中的错误,输出第一个错误. 2.如果没有错误,也就是错误状态都为F ...
- android使用属性动画代替补间动画
本文参考Android属性动画完全解析(上),初识属性动画的基本用法 android3.0之前一共有两种动画,分别是frame动画和tween动画,关于这两种动画如果不了解可以查看我之前的文章andr ...
- HttpModule HttpHandler HttpHandlerFactory 学习笔记
1.HttpModule 最常见的是使用HttpModule来做页面权限控制. 在新建类库添加如下代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...
- VS2015+TFS2015源代码管理
使用Visual Studio连接TFS
- Windows7服务无法启动的解决
这周六,我接到了一个很诡异的案例,表现为任务栏右下角网络连接图标始终为一个红叉,已排除网卡硬件.链路和网卡驱动的问题.主板都新换了一块,可是问题依旧,这无疑将问题的根源指向了操作系统.本想通过网络疑难 ...
- android本地定时通知
android本地通知略有不同,分为立即触发和延时触发 1.即时通知 android默认的Notification为立即触发 Intent intent = new Intent(Intent.ACT ...
- HTML+CSS基础学习笔记(2)
一.无序列表标签ul <ul> <li>信息</li> <li>信息</li> ...... </ul> 代码解释:每项< ...
- MYSQL批处理
待更新 版权声明:本文为博主原创文章,未经博主允许不得转载.
- .NET设计模式(4):建造者模式(Builder Pattern)
):建造者模式(Builder Pattern) .建造者模式的使用使得产品的内部表象可以独立的变化.使用建造者模式可以使客户端不必知道产品内部组成的细节. 2.每一个Builder都相对独立, ...
- 个人博客设计:创建Sql数据库操作类。
整体的博客框架如下 数据库操作java类如下 package com.yxq.dao; import java.sql.Connection; import java.sql.DriverManage ...