Til the Cows Come Home (最短路(Dijkstra,Dijkstra+优先队列,Floyd))

#include <stdio.h>
#include <iostream>
#include <queue>
#include <string.h>
using namespace std;
#define  N 1011
#define INF 0x3f3f3f3f
int n,m;
int mp[N][N];
void init(int n){// 初始化 
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			if(i==j) mp[i][j]=0;
			else mp[i][j]=mp[j][i]=INF;
		}
	}
}
void getmap(int m)//建图 
{
	int u,v,val;
	while(m--)
	{
		scanf("%d%d%d",&u,&v,&val);
		u--,v--;//因为下面的遍历是从0开始,而输入是从1开始 
		mp[u][v]=mp[v][u]=min(mp[u][v],val);// 如果是双向图写这句 
			// mp[u][v] = min(mp[u][v], val); //如果是单向图写这句 
	}
}
bool vis[N];int dis[N];//dis[N]表示st到N的最短距离 
void djk(int st,int end)
{
	int id;
	for(int i=0;i<n;i++)
	{
		vis[i]=false;
		dis[i]=mp[st][i];
	}
	vis[st]=true;
	for(int i=1;i<n;i++)
	{
		int mn=INF,id=-1;
		for(int j=0;j<n;j++)
		{
			if(!vis[j]&&dis[j]<mn){//找到离起点最近的未访问过的点 
				 mn=dis[j];id=j;
			}
		}
	 
		if(id==-1)  break; 
		vis[id]=true;//标记 
		for(int j=0;j<n;j++)
		{
			if(!vis[j]&&mp[id][j]!=INF){//未访问过 id->j有边 
				if(dis[j]>dis[id]+mp[id][j])//是否可以松弛 
				{
					dis[j]=dis[id]+mp[id][j];
				}
		}
	}
}
	printf("%d\n",(dis[end]==INF) ? -1:dis[end]);
}
int main()
{
	int t;
	scanf("%d%d",&t,&n);
	init(n);
	getmap(t);
	djk(n-1,0);
	return 0;
}
二
#include <stdio.h>
#include <queue>
#include <string.h>
using namespace std;
typedef pair<int,int>pii;//这个是c++中特有的,相当于一个结构体有两个元素 
#define N 100011
#define M 1000011
#define INF 0x3f3f3f3f
struct Edge{//边的定义 
	int to,val,next;
	Edge(){}
	Edge(int _to,int _val,int _next){
		to=_to;val=_val;next=_next;
	}
	
}edge[M<<1]; //如果是双向图的话,边的数量是题目中描述的2倍 
int n,m;//n是图中的点数,m 是图中的边数 
int head[N],top;
void init(int n){//初始化 
	memset(head,-1,sizeof(int)*(n+1));//把从head开始的n+1个int类型值设置为-1通常用来初始化数组
	top=0;
}
void add(int u,int v,int val){//加单向边 
	edge[top]=Edge(v,val,head[u]);//head[u]与u同起点上一条边的位置 
	head[u]=top++;//把top的位置赋值给head[u],便于下一次接着使用 
}
void getmap(int m)//建图 
{
	int u,v,val;
	while(m--)
	{
		scanf("%d%d%d",&u,&v,&val);
		add(u,v,val);
		add(v,u,val);//如果是双向图加上这个代码 
	}
}
int dis[N];
void djk(int st,int ed)
{
	memset(dis,0x3f,sizeof(int)*(n+1));
	priority_queue<pii,vector<pii>,greater<pii> > que;//make_pair(dis,v)表示v到起点的距离为dis 
	dis[st]=0;que.push(make_pair(0,st));
	while(!que.empty()) {
		pii p=que.top();que.pop();//每次取离起点最近的 
		int v=p.second;
		if(dis[v]<p.first) continue;
		for(int i=head[v];~i;i=edge[i].next)
		{
			Edge e=edge[i];
			if(dis[e.to]>dis[v]+e.val){
				dis[e.to]=dis[v]+e.val;
				que.push(make_pair(dis[e.to],e.to));
			}
		}
	}
	printf("%d\n",dis[ed]==INF?-1:dis[ed]);
}
int main()
{
	int t;
	scanf("%d%d",&t,&n);
	init(n);
	getmap(t);
	djk(n,1);
	return 0;
}
三
//此代码求多元最短路,邻接矩阵存图 时间复杂度 O(n^3),可求得任意两个点之间的最短距离 
#include <stdio.h>
#include <queue>
#include <string.h>
using namespace std;
#define N 1011
#define INF 0x3f3f3f3f
int n,m;
int mp[N][N];
void init(int n)//初始化 
{
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			if(i==j) mp[i][j]=0;
			else mp[i][j]=mp[j][i]=INF;
		}
	}
}
void getmap(int m)//建图 
{
	int u,v,val;
	while(m--)
	{
		scanf("%d%d%d",&u,&v,&val);
		u--;v--;
		mp[u][v]=mp[v][u]=min(mp[u][v],val);//双向图写这行
		//mp[u][v]=min(mp[u][v],val);//单向写这行		
	}
}
void floyd(int n)//核心代码 点的序号0-n-1; 
{
	for(int k=0;k<n;k++)
	{
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++)
			{
				mp[i][j]=min(mp[i][j],mp[i][k]+mp[k][j]);
			}
		}
	}
}
int main()
{
	int t;
	scanf("%d%d",&t,&n);
	init(n);
	getmap(t);
	floyd(n);
	printf("%d\n",mp[n-1][0]==INF?-1:mp[n-1][0]);
}

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.

猜你喜欢

转载自blog.csdn.net/qq_42434171/article/details/81674611
今日推荐