백 련 / 2017 대학원생 온라인 시험 면제 H: Til the Cows Come Home

3430 단어 백 련 OJ/poj도 론
제목:http://poj.org/problem?id=2387
----------------------------------------
Til the Cows Come Home
총 시간 제한: 1000ms     메모리 제한: 65536kB
묘사 하 다.
Bessie is out in the field and wants to get back to thebarn to get as much sleep as possible before Farmer John wakes her for themorning milking. Bessie needs her beauty sleep, so she wants to get back asquickly as possible.Farmer John's field has N (2 <= N <= 1000) landmarks in it, uniquelynumbered 1..N. Landmark 1 is the barn; the apple tree grove in which Bessiestands all day is landmark N. Cows travel in the field using T (1 <= T <=2000) bidirectional cow-trails of various lengths between the landmarks. Bessieis not confident of her navigation ability, so she always stays on a trail fromits start to its end once she starts it.Given the trails between the landmarks, determine the minimum distance Bessiemust walk to get back to the barn. It is guaranteed that some such routeexists.
입력
* 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. Thethird integer is the length of the trail, range 1..100.
출력
* Line 1: A single integer, the minimum distance thatBessie must travel to get from landmark N to landmark 1.
샘플 입력
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
샘플 출력
90
제시 하 다.
INPUT DETAILS:There are five landmarks.OUTPUT DETAILS:Bessie can get home by following trails 4, 3, 2, and 1.
-----------------------------------------------------
문제 풀이 의 사고 방향.
hep Dijskra 알고리즘, 무 거 운 변 에 주의 하 세 요!무 거 운 쪽 이 있어!무 거 운 쪽 이 있어!
그 밖 에 코드 차원 에서 두 가 지 를 주의해 야 한다.
1. 2 차원 동적 배열 의 성명 과 초기 화
2. operator 의 과부하 (자바 의 comparator interface 와 유사)
-----------------------------------------------------
코드
#include
#include
#include
#include
using namespace std;

struct Node {
	int id;
	int len;
};

struct lenLess {
	bool operator() (const Node &n1, const Node &n2) {
		return n1.len > n2.len;
	}
} len_less;

int main()
{
	int N=0, T=0, i=0, j=0, h=0, t=0, c=0;
	cin >> T >> N;
	int** graph = new int*[N];			//         ,  cost~[1,100],  char  
	for (i=0; i> h >> t >> c;
		h--;								//   C     0     
		t--;
		if (graph[h][t] == 0)				//     
		{
			graph[h][t] = c;
			graph[t][h] = c;
		}
		else								//    
		{
			graph[h][t] = min(graph[h][t], c);	//      
			graph[t][h] = min(graph[t][h], c);	//      
		}
			
	}

	//    
	deque dijk;							// N-1           N-1     
	for (i=0; i::iterator it;
	while (iMin != 0)
	{
		make_heap(dijk.begin(), dijk.end(), len_less);	//      
		Node nMin = dijk.front();						//             
		cMin = nMin.len;
		iMin = nMin.id;
		dijk.pop_front();
		for (it = dijk.begin(); it != dijk.end(); it++)
		{
			if (graph[iMin][it->id] != 0)				//   it->id iMin  
			{
				it->len = min(cMin + graph[iMin][it->id], it->len);	//     
			}
		}
	}

	cout << cMin;

	for (i=0; i

좋은 웹페이지 즐겨찾기