HDOJ水题集合2:最短路(Dijkstra)

举报
小哈里 发表于 2022/05/11 00:32:22 2022/05/11
【摘要】 1001 畅通工程续 HDOJ1874 裸Dijkstra 畅通工程续 Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K...

1001 畅通工程续 HDOJ1874 裸Dijkstra

畅通工程续
Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 33 Accepted Submission(s) : 20
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
某省自从实行了很多年的畅通工程计划后,终于修建了很多路。不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多。这让行人很困扰。

现在,已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离。
Input
本题目包含多组数据,请处理到文件结束。
每组数据第一行包含两个正整数N和M(0<N<200,0<M<1000),分别代表现有城镇的数目和已修建的道路的数目。城镇分别以0~N-1编号。
接下来是M行道路信息。每一行有三个整数A,B,X(0<=A,B<N,A!=B,0<X<10000),表示城镇A和城镇B之间有一条长度为X的双向道路。
再接下一行有两个整数S,T(0<=S,T<N),分别代表起点和终点。
Output
对于每组数据,请在一行里输出最短需要行走的距离。如果不存在从S到T的路线,就输出-1.
Sample Input
3 3
0 1 1
0 2 3
1 2 1
0 2
3 1
0 1 1
1 2
Sample Output
2
-1
Author
linle
Source
2008浙大研究生复试热身赛(2)——全真模拟

#include<bits/stdc++.h>
using namespace std;
const int maxn = 210, inf=1e9+10;
int e[maxn][maxn];
int dist[maxn], vis[maxn];
int main(){
	ios::sync_with_stdio(false);
	int n, m;
	while(cin>>n>>m){
		memset(e,0x3f,sizeof(e));
		//for(int i = 0; i < n; i++)e[i][i] = 0;
		for(int i = 0; i < m; i++){
			int u, v, w; cin>>u>>v>>w;
			if(e[u][v]>w)e[u][v] = e[v][u] = w;
		}
		int s, t;  cin>>s>>t;
		
		//for(int i = 0; i < n; i++)dist[i] = e[s][i];
		memset(dist,0x3f,sizeof(dist));
		memset(vis,0,sizeof(vis));
		dist[s] = 0;
		for(int i = 1; i <= n; i++){
			int u = -1, minn = inf;
			for(int j = 0; j < n; j++){
				if(!vis[j] && dist[j]<minn){
					u = j; minn = dist[j];
				}
			}
			//cout<<u<<"\n";
			if(u==-1)break;
			vis[u] = 1;
			for(int j = 0; j < n; j++){
				if(dist[j]>dist[u]+e[u][j]){
					dist[j] = dist[u]+e[u][j];
				}
			}
		}
		if(dist[t]!=e[201][201])cout<<dist[t]<<"\n";
		else cout<<-1<<"\n";
		//for(int i = 0; i < n; i++)cout<<dist[i]<<" ";cout<<"\n";
	}
	return 0;
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

1002 一个人的旅行 HDOJ2066 裸Dijkstra+新建起点终点

一个人的旅行
Time Limit : 1000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 43 Accepted Submission(s) : 17
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,0),很多事,还能丰富自己的阅历,还可以看美丽的风景……草儿想去很多地方,她想要去东京铁塔看夜景,去威尼斯看电影,去阳明山上看海芋,去纽约纯粹看雪景,去巴黎喝咖啡写信,去北京探望孟姜女……眼看寒假就快到了,这么一大段时间,可不能浪费啊,一定要给自己好好的放个假,可是也不能荒废了训练啊,所以草儿决定在要在最短的时间去一个自己想去的地方!因为草儿的家在一个小镇上,没有火车经过,所以她只能去邻近的城市坐火车(好可怜啊~)。
Input
输入数据有多组,每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有S个,草儿想去的地方有D个;
接着有T行,每行有三个整数a,b,time,表示a,b城市之间的车程是time小时;(1=<(a,b)<=1000;a,b 之间可能有多条路)
接着的第T+1行有S个数,表示和草儿家相连的城市;
接着的第T+2行有D个数,表示草儿想去地方。
Output
输出草儿能去某个喜欢的城市的最短时间。
Sample Input
6 2 3
1 3 5
1 4 7
2 8 12
3 8 4
4 9 12
9 10 2
1 2
8 9 10
Sample Output
9
Author
Grass
Source
RPG专场练习赛

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1010, inf=1e9+10;
int e[maxn][maxn];
int dist[maxn], vis[maxn];
int main(){
	ios::sync_with_stdio(false);
	int n, m, s, d;
	while(cin>>m>>s>>d){
		n = 0;
		memset(e,0x3f,sizeof(e));
		for(int i = 0; i < m; i++){
			int u, v, w; cin>>u>>v>>w;
			if(e[u][v]>w)e[u][v] = e[v][u] = w;
			n = max(n,max(u,v));
		}
		for(int i = 1; i <= s; i++){
			int x;  cin>>x;  e[x][0]=e[0][x]=0;
		}
		for(int i = 1; i <= d; i++){
			int x;  cin>>x;  e[x][n+1]=e[n+1][x]=0;
		}
		
		memset(dist,0x3f,sizeof(dist));
		memset(vis,0,sizeof(vis));
		dist[0] = 0;
		for(int i = 0; i <= n; i++){
			int u = -1, minn = inf;
			for(int j = 0; j <= n+1; j++){
				if(!vis[j] && dist[j]<minn){
					u = j; minn = dist[j];
				}
			}
			//cout<<u<<"\n";
			if(u==-1)break;
			vis[u] = 1;
			for(int j = 0; j <= n+1; j++){
				if(dist[j]>dist[u]+e[u][j]){
					dist[j] = dist[u]+e[u][j];
				}
			}
		}
		if(dist[n+1]!=e[1001][1001])cout<<dist[n+1]<<"\n";
		else cout<<-1<<"\n";
		//for(int i = 0; i < n; i++)cout<<dist[i]<<" ";cout<<"\n";
	}
	return 0;
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

1003 最短路 HDOJ2544 裸Dijkstra

最短路
Time Limit : 5000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 24 Accepted Submission(s) : 15
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt。但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你可以帮助他们吗?

Input
输入包括多组数据。每组数据第一行是两个整数N、M(N<=100,M<=10000),N表示成都的大街上有几个路口,标号为1的路口是商店所在地,标号为N的路口是赛场所在地,M则表示在成都有几条路。N=M=0表示输入结束。接下来M行,每行包括3个整数A,B,C(1<=A,B<=N,1<=C<=1000),表示在路口A与路口B之间有一条路,我们的工作人员需要C分钟的时间走过这条路。
输入保证至少存在1条商店到赛场的路线。
Output
对于每组输入,输出一行,表示工作人员从商店走到赛场的最短时间
Sample Input
2 1
1 2 3
3 3
1 2 5
2 3 5
3 1 2
0 0
Sample Output
3
2

Source
UESTC 6th Programming Contest Online

#include<bits/stdc++.h>
using namespace std;
const int maxn = 110, inf=1e9+10;
int e[maxn][maxn];
int dist[maxn], vis[maxn];
int main(){
	ios::sync_with_stdio(false);
	int n, m, s, d;
	while(cin>>n>>m &&n&&m){
		memset(e,0x3f,sizeof(e));
		for(int i = 1; i <= m; i++){
			int u, v, w; cin>>u>>v>>w;
			if(e[u][v]>w)e[u][v] = e[v][u] = w;
		}
		
		memset(dist,0x3f,sizeof(dist));
		memset(vis,0,sizeof(vis));
		dist[1] = 0;
		for(int i = 1; i <= n; i++){
			int u = -1, minn = inf;
			for(int j = 1; j <= n; j++){
				if(!vis[j] && dist[j]<minn){
					u = j; minn = dist[j];
				}
			}
			if(u==-1)break;
			vis[u] = 1;
			for(int j = 1; j <= n; j++){
				if(dist[j]>dist[u]+e[u][j]){
					dist[j] = dist[u]+e[u][j];
				}
			}
		}
		cout<<dist[n]<<"\n";
		//for(int i = 0; i < n; i++)cout<<dist[i]<<" ";cout<<"\n";
	}
	return 0;
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

1004 最短路径问题 HDOJ3790 裸Dijkstra+双边权

最短路径问题
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 51 Accepted Submission(s) : 15
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的。
Input
输入n,m,点的编号是1~n,然后是m行,每行4个数 a,b,d,p,表示a和b之间有一条边,且其长度为d,花费为p。最后一行是两个数 s,t;起点s,终点。n和m为0时输入结束。
(1<n<=1000, 0<m<100000, s != t)
Output
输出 一行有两个数, 最短距离及其花费。
Sample Input
3 2
1 2 5 6
2 3 4 5
1 3
0 0
Sample Output
9 11
Source
浙大计算机研究生复试上机考试-2010年

#include <bits/stdc++.h>
using namespace std;
const int maxn=1010;
const int inf = 99999999;

int n, m, s, t;
int e[maxn][maxn], e2[maxn][maxn];
int dist[maxn], cost[maxn], vis[maxn];

int main(){
	ios::sync_with_stdio(false);
	while(cin>>n>>m &&n+m){
		memset(e,0x3f,sizeof(e));
		memset(e2,0x3f,sizeof(e2));
		for(int i = 1; i <= m; i++){
			int a, b, w, w2;
			cin>>a>>b>>w>>w2;
			if(e[a][b]>w){
				e[a][b] = e[b][a] = w;
				e2[a][b] = e2[b][a] = w2;
			}
			if(e[a][b]==w && e2[a][b]>w2){
				e2[a][b] = e2[b][a] = w2;
			}
		}
		cin>>s>>t;
		memset(dist,0x3f,sizeof(dist));
		memset(cost,0x3f,sizeof(cost));
		memset(vis,0,sizeof(vis));
		dist[s] = cost[s] = 0;
		for(int i = 1; i <= n; i++){
			int u = -1, minn = inf;
			for(int j = 1; j <= n; j++){
				if(!vis[j] && dist[j]<minn)
					{ u = j; minn = dist[j]; }
			}
			vis[u] = 1;
			for(int j = 1; j <= n; j++){
				if(dist[j]>dist[u]+e[u][j]){
					dist[j]=dist[u]+e[u][j];
					cost[j]=cost[u]+e2[u][j];
				}else if(dist[j]==dist[u]+e[u][j] && cost[j]>cost[u]+e2[u][j]){
					cost[j]=cost[u]+e2[u][j];
				}
			}
		}
		cout<<dist[t]<<" "<<cost[t]<<"\n";
	}
	return 0;
}



  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

文章来源: gwj1314.blog.csdn.net,作者:小哈里,版权归原作者所有,如需转载,请联系作者。

原文链接:gwj1314.blog.csdn.net/article/details/114991896

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。