USACO comehome Dijkstra

USER: Kevin Samuel [kevin_s1]
TASK: comehome
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.003 secs, 3376 KB]
   Test 2: TEST OK [0.005 secs, 3376 KB]
   Test 3: TEST OK [0.005 secs, 3376 KB]
   Test 4: TEST OK [0.005 secs, 3376 KB]
   Test 5: TEST OK [0.011 secs, 3376 KB]
   Test 6: TEST OK [0.019 secs, 3376 KB]
   Test 7: TEST OK [0.035 secs, 3376 KB]
   Test 8: TEST OK [0.005 secs, 3376 KB]
   Test 9: TEST OK [0.008 secs, 3376 KB]

All tests OK.

YOUR PROGRAM (‘comehome‘) WORKED FIRST TIME!  That‘s fantastic
-- and a rare thing.  Please accept these special automated

congratulations.

very easy, it‘s a shortest path problem

建图时直接以A-Z和a-z作为节点建图,我用了一个简单的Dijkstra算法,从Z 点反向找最近的点
/*
ID:kevin_s1
PROG:comehome
LANG:C++
*/

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <list>
#include <cmath>

using namespace std;

#define MAXN 53
#define INF 99999

//gobal variable====

int P;
int Graph[MAXN][MAXN];
int result;
int Dist[MAXN];

int visited[MAXN];
//==================

//function==========
int Convert(char ch){
	int index = -1;
	if(ch >= 'A' && ch <= 'Z'){
		index = ch - 'A' + 1;
	}
	else if(ch >= 'a' && ch <= 'z'){
		index = ch - 'a' + 27;
	}
	else
		index = -1;
	return index;
}

int Dijkstra(int v0){
	int min;
	int k;
	//init
	for(int i = 1; i < MAXN; i++){
		visited[i] = false;
		Dist[i] = Graph[v0][i];
	}
	Dist[v0] = 0;
	visited[v0] = 1;
	for(int v = 1; v < MAXN; v++){
		min = INF;
		for(int w = 1; w < MAXN; w++){
			if(!visited[w] && (Dist[w] < min)){
				min = Dist[w];
				k = w;
			}
		}
		visited[k] = 1;
		for(int w = 1; w < MAXN; w++){
			if(!visited[w] && (min + Graph[k][w] < Dist[w])){
				Dist[w] = min + Graph[k][w];
			}
		}
	}
	int Min = INF;
	for(int i = 1; i < MAXN; i++){
		if(Dist[i] < Min && i >= 1 && i <= 26 && Dist[i] > 0){
			Min = Dist[i];
			result = i;
		}
	}
	return Min;
}

//==================

int main(){
	freopen("comehome.in","r",stdin);
	freopen("comehome.out","w",stdout);
	cin>>P;

	for(int i = 1; i < MAXN; i++){
		Graph[i][i] = INF;
		for(int j = 1; j < MAXN; j++){
			Graph[i][j] = INF;
		}
	}
	memset(visited, 0, sizeof(visited));
	while(P--){
		char chst, chen;
		int dist;
		cin>>chst>>chen>>dist;
		int st = Convert(chst);
		int en = Convert(chen);
		if(dist < Graph[st][en]){
			Graph[st][en] = dist;
			Graph[en][st] = dist;
		}
	}
	int ans = Dijkstra(26);
	char chfarm = 'A' + result - 1;
	cout<<chfarm<<" ";
	cout<<ans<<endl;
	return 0;
}

USACO comehome Dijkstra

时间: 2024-07-31 04:44:58

USACO comehome Dijkstra的相关文章

【日常学习】【最短路Dijkstra】codevs1069 usaco回家 题解

来源 usaco codevs1069 题目描述 Description 现在是晚餐时间,而母牛们在外面分散的牧场中. 农民约翰按响了电铃,所以她们开始向谷仓走去. 你的工作是要指出哪只母牛会最先到达谷仓(在给出的测试数据中,总会有且只有一只最快的母牛). 在挤奶的时候(晚餐前),每只母牛都在她自己的牧场上,一些牧场上可能没有母牛. 每个牧场由一条条道路和一个或多个牧场连接(可能包括自己). 有时,两个牧场(可能是字母相同的)之间会有超过一条道路相连. 至少有一个牧场和谷仓之间有道路连接. 因此

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-3268-最短路(dijkstra算法)

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12494   Accepted: 5568 Description 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 farm #X (1 ≤ X

洛谷 P1529 回家 Bessie Come Home Label:Dijkstra最短路 &amp;&amp; 乱搞

题目描述 现在是晚餐时间,而母牛们在外面分散的牧场中. 农民约翰按响了电铃,所以她们开始向谷仓走去. 你的工作是要指出哪只母牛会最先到达谷仓(在给出的测试数据中,总会有且只有一只最快的母牛). 在挤奶的时候(晚餐前),每只母牛都在她自己的牧场上,一些牧场上可能没有母牛. 每个牧场由一条条道路和一个或多个牧场连接(可能包括自己). 有时,两个牧场(可能是字母相同的)之间会有超过一条道路相连. 至少有一个牧场和谷仓之间有道路连接. 因此,所有的母牛最后都能到达谷仓,并且母牛总是走最短的路径. 当然,

USACO concom DFS

写哭了,本来感觉是floyd,但是发现floyd根本不能连续地传递,然后看了题解写了个搜索,这个搜索我都没有想到= = 先贴个floyd的代码,先试图用DFS处理连续控股的情况,再用几个循环处理k1+k2+k3+...Kn 在第八组数据跪了 /* ID:kevin_s1 PROG:concom LANG:C++ */ #include <iostream> #include <cstdio> #include <string> #include <cstring&

POJ 3268 双向Dijkstra

Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13020 Accepted: 5832 Description 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 farm #X (1 ≤ X ≤ N)

POJ 3662 Telephone Lines【Dijkstra最短路+二分求解】

Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7214   Accepted: 2638 Description Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncooperative, so he needs to pay for some of

【日常学习】【Dijkstra堆优化】codevs2038 香甜的黄油题解

转载请注明出处 [ametake版权所有]http://blog.csdn.net/ametake 先放上题目,出自USACO 题目描述 Description 农夫John发现做出全威斯康辛州最甜的黄油的方法:糖.把糖放在一片牧场上,他知道N(1<=N<=500)只奶牛会过来舔它,这样就能做出能卖好价钱的超甜黄油.当然,他将付出额外的费用在奶牛上. 农夫John很狡猾.他知道他可以训练这些奶牛,让它们在听到铃声时去一个特定的牧场.他打算将糖放在那里然后下午发出铃声,以至他可以在晚上挤奶. 农

POJ 2394 Checking an Alibi (最短路+Dijkstra)

Checking an Alibi Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6217   Accepted: 2257 Description A crime has been comitted: a load of grain has been taken from the barn by one of FJ's cows. FJ is trying to determine which of his C (1