poj 2387 Til the Cows Come Home -- 最短路dijstra

<span style="font-size:18px;">利用dijstra求单源最短路,利用floyd会超时的</span>


#include <stdio.h>
#include <string.h>
#include <algorithm>
#define inf 0x3f3f3f3f
using namespace std;
const int N = 1005;
int map[N][N];
int vis[N];
int dis[N];
int m,n;
void dij(){
	int i,j;
	memset(vis,0,sizeof(vis));
	for(i=1;i<=n;i++){
		if(i==1) dis[i]=0;
		else
			dis[i]=map[1][i];
	}
	vis[1]=1;
	for(i=2;i<=n;i++){
		int m=inf,t;
		for(j=1;j<=n;j++){
			if(!vis[j]&&dis[j]<m){
				t=j;
				m=dis[j];
			}
		}
		vis[t]=1;
		for(j=1;j<=n;j++){
			if(!vis[j]&&dis[j]>dis[t]+map[t][j])
				dis[j]=dis[t]+map[t][j];
		}
	}
}
int main(){
	int i,j;
	while(~scanf("%d %d",&m,&n)){
		memset(map,inf,sizeof(map));
		for(i=1;i<=m;i++){
			int x,y,z;
			scanf("%d %d %d",&x,&y,&z);
			map[x][y]=min(map[x][y],z);
			map[y][x]=min(map[y][x],z);
		}
		dij();
		printf("%d\n",dis[n]);
	}
	return 0;
}

				
时间: 2024-10-11 16:43:59

poj 2387 Til the Cows Come Home -- 最短路dijstra的相关文章

POJ 2387 Til the Cows Come Home (最短路+Dijkstra)

Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29550   Accepted: 9935 Description 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

POJ 2387 Til the Cows Come Home dijkstra算法 用邻接表和邻接矩阵

题目如下: Til the Cows Come Home Time Limit: 1000MS        Memory Limit: 65536K Total Submissions: 27726        Accepted: 9353 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wa

poj 2387 Til the Cows Come Home(dijkstra算法)

题目链接:http://poj.org/problem?id=2387 题目大意:起点一定是1,终点给出,然后求出1到所给点的最短路径. 注意的是先输入边,在输入的顶点数,不要弄反哦~~~ 1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 int map[2010][2010],Min,node[2010],vis[2010],t,q; 5 const int INF=9999999; 6 7 vo

[2016-04-02][POJ][2387][Til the Cows Come Home]

时间:2016-04-02 10:34:36 星期六 题目编号:[2016-04-02][POJ][2387][Til the Cows Come Home] 题目大意:给定n个节点和t条路,求n到1的最短路长度 分析:跑一次最短路即可 遇到的问题: 据说是多重边,如果是用邻接矩阵的就要更新最小值, 此题是先输入t,再输入n,输入的时候读错,无限WA- #include <queue> #include <cstring> #include <cstdio> using

POJ 2387 Til the Cows Come Home (Dijkstra)

题目链接:POJ 2387 Description 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 possib

POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)

传送门 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 46727   Accepted: 15899 Description 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

POJ 2387 Til the Cows Come Home Dijkstra求最短路径

Til the Cows Come Home 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.

怒学三算法 POJ 2387 Til the Cows Come Home (Bellman_Ford || Dijkstra || SPFA)

Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 33015   Accepted: 11174 Description 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

POJ 2387 Til the Cows Come Home

题目连接: http://poj.org/problem?id=2387 Description 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 ba