2021-01-30

最短路思想:看似和搜索中的找最短路径相似,但是最短路思想是直接告诉你两个段点之间是否联通以及他们之间的距离。

1.floyd搜索方法本质就是dp,是多源的最短路思想,但是时间复杂度高,是O(N^3),适用于较少元素的最短路径求解。


void Floyd()
{
	for(int k = 1; k <= n; k++)//注意,一定要先枚举中转节点保证三角形的情况
		for(int i = 1; i <= n; i++)
			for(int j = 1; j <= n; j++)
				dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
}

放到例题。
在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的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

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <queue>
#include <string>
using namespace std;
typedef  long long ll;
const int MAXN=1e3;
int inf = 0x3f3f3f3f,x,y,z;
int dis[MAXN][MAXN],n,m;
void floyd() {
	for(int k=1;k<=n;k++){
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
			}
		}
	}
}
int main() {
	while(~scanf("%d %d",&n,&m),n||m) {
		memset(dis,inf,sizeof dis);
       for(int i=1;i<=n;i++){
       	dis[i][i]=0; 
	   }
	   for(int i=1;i<=m;i++){
	   	scanf("%d %d %d ",&x,&y,&z);
	   	dis[x][y]=dis[y][x]=min(dis[x][y],z);
	   }
     floyd();
	 	printf("%d\n",dis[1][n]);
	} 

}

2.dijkstra思想是在当前的最短路径下再继续展开节点,搜索下去。


void dijkstra()
{
	memset(dis, 0x3f, sizeof dis);
	memset(vis, 0, sizeof vis);
	dis[1] = 0;
	for(int i = 1; i < n; i++)
	{
		int x = 0;//当前dis最小的点
		for(int j = 1; j <= n; j++)//vis的作用是保证每个点全局只被用来更新别的点一次。 
			if(!vis[j] && (x == 0 || dis[j] < dis[x])) x = j; 
			
		vis[x] = 1;
		for(int j = 1; j <= n; j++)//当然,这里用邻接表的话也可以省一些时间和空间
			dis[j] = min(dis[j], dis[x] + g[x][j]); //g是邻接矩阵 
	}
}

放道例题
Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible.

Farmer John’s field has N (2 <= N <= 1000) landmarks in it, uniquely numbered 1…N. Landmark 1 is the barn; the apple tree grove in which Bessie stands all day is landmark N. Cows travel in the field using T (1 <= T <= 2000) bidirectional cow-trails of various lengths between the landmarks. Bessie is not confident of her navigation ability, so she always stays on a trail from its start to its end once she starts it.

Given the trails between the landmarks, determine the minimum distance Bessie must walk to get back to the barn. It is guaranteed that some such route exists.

Input

  • Line 1: Two integers: T and N

  • Lines 2…T+1: Each line describes a trail as three space-separated integers. The first two integers are the landmarks between which the trail travels. The third integer is the length of the trail, range 1…100.

Output

  • Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.

Sample Input
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100

Sample Output
90
Hint

INPUT DETAILS:

There are five landmarks.

OUTPUT DETAILS:

Bessie can get home by following trails 4, 3, 2, and 1.

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <queue>
#include <string>
using namespace std;
typedef  long long ll;
const int MAXN=1e3+10;
int d[MAXN],v[MAXN],dis[MAXN][MAXN];
int inf = 0x3f3f3f3f,t,n,i,j,k,a,b,c,x,y,m;
void dijkstra() {
    memset(d,inf,sizeof d);
	d[1]=0; 
	memset(dis,inf,sizeof dis);
	memset(v,0,sizeof v);//初始化 
	for(i=1; i<=t; i++) {
		scanf("%d%d%d",&a,&b,&c);
		dis[a][b]=dis[b][a]=min(dis[a][b],c);//如果路有重复取最短 
	}
	for(i=1; i<=n; i++) {
		m=inf;//依次找最短距离 
		for(j=1; j<=n; j++) {
			if(v[j]==0&&d[j]<m) {
				x=j;
				m=d[x];
			}
		}
		v[x]=1;//这个点以经找过了 
		for(k=1; k<=n; k++) {
			d[k]=min(d[k],d[x]+dis[x][k]);//从之前的最短距离开始往上加; 
		}
	}
}
int main() {
	scanf("%d %d",&t,&n);
	dijkstra();

	printf("%d\n",d[n]);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值