hdu2680 choose the best route
题意:给定一个有向图,多个起点,一个终点,求起点到终点的最短路。
这道题TLE了好多次,两侧次的对比主要在于对起点的处理上,法一:最开始是采用的hdu2066——一个人的旅行,这道题的方法做的,发现总是TLE。
法二:然后看别人的博客:再加上一个点作为起点,编号为0,这个点和题中给的那些起点之间的距离为0。这样题目就转化为了求单源最短路径问题。
当时我觉得这两种做法没有什么不同,结果我错了。法一:每个起点都要去运行一边 Dijkstra算法,而法二:只用运行一次Dijkstra算法,所以法一总是超时。
接下来给出两次的代码:
法一:
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
const int Max = 1010;
const int INF = 0xFFFFFFF;
int value[Max][Max];
int vis[Max];
int dis[Max];
int s[Max];
int T,S,D;
void init(){
for(int i = 1 ; i <= T ; i++){
for(int j = 1 ; j <= T ; j++)
value[i][j] = INF;//初始化点与点之间的距离
}
}
void Dijkstra(int s)
{
int pos;//?
int i,j;
memset(vis,0,sizeof(vis));
for( i=1;i<=T;i++)
dis[i]=INF;
dis[s]=0;
for( i = 1;i<=T;i++)
{
pos = -1;
for( j=1;j<=T;j++)
{
if(!vis[j]&&(pos==-1||dis[j]<dis[pos]))
pos=j;//?
}
if(pos == -1)
break;
vis[pos]=1;
for( j=1;j<=T;j++)
{
if(!vis[j]&&dis[j]>dis[pos]+value[pos][j])
dis[j]=dis[pos]+value[pos][j];
}
}
}
int main()
{
int a,b,v,ans,i;
while(~scanf("%d%d%d",&T,&S,&D))
{
init();
memset(s,0,sizeof(s));
// for(int i=1;i<=T;i++)
// v[i]=i;
for( i=0; i<S; i++)
{
scanf("%d%d%d",&a,&b,&v);
if(value[a][b] > v) //要是有相同边,保存最短的边
value[a][b]=v;
}
int w;
scanf("%d",&w);
for( i=0;i<w;i++)
scanf("%d",&s[i]);
//枚举
ans = INF;
for( i=0;i<w;i++)
{
Dijkstra(s[i]);
ans = ans<dis[D]? ans : dis[D];
}
if(ans!=INF)
printf("%d\n",ans);
else
printf("-1\n");
}
return 0;
}
法二:
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
const int Max = 1010;
const int INF = 0xFFFFFFF;
int value[Max][Max];
int vis[Max];
int dis[Max];
int T,S,D;
void init(){
for(int i = 0 ; i <= T ; i++){
for(int j = 0 ; j <= T ; j++)
value[i][j] = INF;//初始化点与点之间的距离
}
}
void Dijkstra(int s)
{
int pos;//?
int i,j;
memset(vis,0,sizeof(vis));
for( i=0;i<=T;i++)
dis[i]=INF;
dis[s]=0;
for( i = 0;i<=T;i++)
{
pos = -1;
for( j=0;j<=T;j++)
{
if(!vis[j]&&(pos==-1||dis[j]<dis[pos]))
pos=j;//?
}
if(pos == -1)
break;
vis[pos]=1;
for( j=0;j<=T;j++)
{
if(!vis[j]&&dis[j]>dis[pos]+value[pos][j])
dis[j]=dis[pos]+value[pos][j];
}
}
}
int main()
{
int a,b,v,i;
int s;
while(~scanf("%d%d%d",&T,&S,&D))
{
init();
for( i=0; i<S; i++)
{
scanf("%d%d%d",&a,&b,&v);
if(value[a][b] > v) //要是有相同边保存最短的边
value[a][b]=v;
}
int w;
scanf("%d",&w);
for(i=0;i<w;i++)
{
scanf("%d",&s);
value[0][s]=0;
}
Dijkstra(0);
if(dis[D]!=INF)
printf("%d\n",dis[D]);
else
printf("-1\n");
}
return 0;
}
hdu2680 choose the best route的更多相关文章
- 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 ...
- 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(最短路)
题目链接: Choose the best route Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- HDU-2680 Choose the best route 单向边+反向dijkstra
https://vjudge.net/problem/HDU-2680 题意:以起始点 终点 长度 给出一个图,已知可以从w个起点出发,求从任一起点到同一个终点s的最短路径.注意是单向边.m<1 ...
- hdu2680 Choose the best route 最短路(多源转单源)
此题中起点有1000个,边有20000条.用链式前向星建图,再枚举起点用SPFA的话,超时了.(按理说,两千万的复杂度应该没超吧.不过一般说计算机计算速度 1~10 千万次/秒.也许拿最烂的计算机来卡 ...
- hdu 2680 Choose the best route
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2680 Choose the best route Description One day , Kiki ...
- Choose the best route(最短路)dijk
http://acm.hdu.edu.cn/showproblem.php?pid=2680 Choose the best route Time Limit: 2000/1000 MS (Java/ ...
- 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 ( ...
- 最短路问题-- Dijkstra Choose the best route
Choose the best route Problem Description One day , Kiki wants to visit one of her friends. As she i ...
随机推荐
- commonCookie.js
/** * Created with JetBrains WebStorm. * NAME: commonCookie.js */(function(window,document){ var com ...
- java 包的命名规范
- IntelliJ IDEA project module
在IDEA 创建一个project,目录结构是这样的:在project下创建一个module之后目录结构是这样的: 简单的概括如下: IntelliJ系中的 Project 相当于Eclipse系中 ...
- __init__和__new__,以及self
__new__: 每次实例化类的时候会默认先执行__new__,进行实例化类. 如果想改变默认__new__行为,可以对它进行自定义,必须有返回实例. __init__: 对实例化的类进行初始化, ...
- 关于oracle数据库
Oracle数据库是做什么的? oracle数据库和其他数据库一样,都是保存数据的,同时可以去查询,修改,删除等oracle和其他数据不一样的地方在于,它又复杂的机制可以保证在数据库服务器突然坏了的情 ...
- Windows下Fortran编译Lapack库及使用的方法(转自新浪)
Lapack 是一套被非常广泛使用的线性代数计算工具库,现在的主要编写语言时 Fortran90 .Lapack 基本上可以说是速度最快的线性代数计算库,我们看看官方给出的其被应用的几个地方就可以看出 ...
- Python3 bytes 函数
Python3 bytes 函数 Python3 内置函数 描述 bytes 函数返回一个新的 bytes 对象,该对象是一个 0 <= x < 256 区间内的整数不可变序列.它是 b ...
- Codeforces Beta Round #9 (Div. 2 Only)
Codeforces Beta Round #9 (Div. 2 Only) http://codeforces.com/contest/9 A gcd水题 #include<bits/stdc ...
- IO模型与select,poll,epoll
五种:阻塞,非阻塞,IO复印,信号驱动,异步. select,poll,epoll select: 典型用32个32位的整数表示1024个描述符,并发的局限. poll:功能同上,但数据结构不一样(链 ...
- day12:vcp考试
Q221. An administrator is creating a new Platform Service Controller Password Policy with the follow ...