1. 程式人生 > >Poj 2387 Til the Cows Come Home (dijkstra + 判重)

Poj 2387 Til the Cows Come Home (dijkstra + 判重)

題意:給你一個無向圖,讓你求1到n的最短距離。

題解:題目描述的很清楚,但是這道題有點小坑,就是兩個點之間會有多條邊。對於多條邊,我們存最小的即可了。然後裸的一個dijkstra就可以了。詳情看程式碼註解

#include<cstring>
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 2005;  //點的個數 
const int inf = 1e9;  

int map[maxn][maxn]; // 鄰接矩陣 
int n,m;

void dijkstra(){
	int minn ,v;
	int dis[maxn];
	bool vis[maxn];
	for(int i = 1 ; i <= n ; i++){  // 初始化 
		vis[i] = 0;
		dis[i] = map[1][i];
	}
	for(int i = 1; i <= n ; i++ ){
		minn = inf;
		for(int j = 1;j <= n ; j ++){     // 找出最短邊 
			if(!vis[j] && dis[j] < minn){
				v = j;
				minn = dis[j];
			}
		}
		vis[v] = 1;
		for(int j = 1; j <= n ; j ++)       // 鬆弛 
			if(!vis[j] && dis[j] > dis[v] + map[v][j])
				dis[j] = dis[v] + map[v][j];
	}
	cout << dis[n] << endl;
}
int main(){
	while(~scanf("%d%d",&m,&n)){           //初始化 
		for(int i = 1; i <= n ; i++)
			for(int j = 1;j <= n ; j ++)
				if(i == j)
					map[i][j] = 0;
				else 
					map[i][j] = map[j][i] = inf;
		int u,v,w;
		for(int i = 1; i <= m ; i++){  //讀入邊 
			cin >> u >> v >> w;
			if(map[u][v] > w)   // 判重 
				map[u][v] = map[v][u] = w;
		}
		dijkstra();
	}
	return 0;
}

相關推薦

Poj 2387 Til the Cows Come Home dijkstra +

題意:給你一個無向圖,讓你求1到n的最短距離。 題解:題目描述的很清楚,但是這道題有點小坑,就是兩個點之間會有多條邊。對於多條邊,我們存最小的即可了。然後裸的一個dijkstra就可以了。詳情看程式碼

POJ-2387-Til the Cows Come Home最短路

which string 算法 ble pri nim distance cows route Description Bessie is out in the field and wants to get back to the barn to get as much s

(階段三 dijkstra演算法溫習 1.6)POJ 2387 Til the Cows Come Home(使用dijkstra演算法求單源起點和單源終點的最短路徑)

/* * HDU_1874_1.cpp * * Created on: 2013年11月10日 * Author: Administrator */ #include <

【最短路各種方法求解一題模板題POJ 2387 Til the Cows Come Home

Problem Description 輸入T,N分別代表有T條通道,和N個地點。接下來T行u,v,w分別表示u地點於v地點之間通道消費,有重複邊 Sample Input 5 5 1 2 20 2 3 30

POJ 2387Til the Cows Come HomeDij最短路

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

POJ - 2387 Til the Cows Come Home

unique uic ring star eterm and string app lang Bessie is out in the field and wants to get back to the barn to get as much sleep as possi

POJ 2387 Til the Cows Come Home

tail from nal rail pst cows clas c代碼 == 題目連接: http://poj.org/problem?id=2387 Description Bessie is out in the field and wants to get back

POJ 2387 Til the Cows Come Home(最短路模板)

ont algorithm inf pre nbsp span pro push_back 城市 題目鏈接:http://poj.org/problem?id=2387 題意:有n個城市點,m條邊,求n到1的最短路徑。n<=1000; m<=2000   就

POJ-2387 Til the Cows Come Home

最短路問題 題目大意: 有好多相通的路,路都有一定的長度,求1 到 n 的最短距離,單源最短路 dijkstra : 思路就是貪心 從節點1 找 ,如果 節點i 到1 的距離 > 節點1到節點u + 節點u和節點i的距離 ,更新路徑權值,這個演

POJ 2387-Til the Cows Come Home(最短路問題)

POJ 2387-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 befor

POJ 2387 Til the Cows Come Home

最近陪XY在練習程式碼,隨便做的一些裸題。 題意:給出一個N個點T條邊的無向圖,求N到1的最短路。 思路:其實直接用最短路演算法也可以,但是因為練習DFS,而且N比較小,就寫了DFS。 程式碼: #include <stdio.h> #i

Dijkstra例題:POJ 2387(Til the Cows Come Home)

#include <stdio.h>#include <iostream>#include <stdlib.h>#include<algorithm>#include<cstring>#include<queue>#define MAX

Til the Cows Come HomeDijkstra

pan std ble book amp TP col AC home Dijkstra (迪傑斯特拉)最短路算法,算是模板 POJ - 2387 1 #include<iostream> 2 #include<algorithm> 3 #

Til the Cows Come Home POJ 2387 簡單最短路 Dijkstra

problem 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 - 2387Til the Cows Come Home最短路徑 Dijkstra演算法

Til the Cows Come Home 大奶牛很熱愛加班,他和朋友在凌晨一點吃完海底撈後又一個人回公司加班,為了多加班他希望可以找最短的距離回到公司。深圳市裡有N個(2 <= N <= 1000)個公交站,編號分別為1..N。深圳是大城市,公交車整天跑跑跑。公交站1是大奶牛的位置,公司所在

Til the Cows Come Home 最短路問題, 模板

題目:https://vjudge.net/problem/POJ-2387  Bessie is out in the field and wants to get back to the barn to get as much sleep as possible befor

Til the Cows Come Home 有向圖最短路徑問題

One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at far

Til the Cows Come Home單源最短路Dijstra模板題

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 he

Til the Cows Come Home 從n到1最短路

題目連線 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 forthe mo

Til the Cows Come Home最短路,注意

DescriptionBessie 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