[usaco]Bessie Come Home

简介: <p>Kolstad & Burch <br> It's dinner time, and the cows are out in their separate pastures. Farmer John rings the bell so they will start walking to the barn. Your job is to figure out which o

Kolstad & Burch
It's dinner time, and the cows are out in their separate pastures. Farmer John rings the bell so they will start walking to the barn. Your job is to figure out which one cow gets to the barn first (the supplied test data will always have exactly one fastest cow).

Between milkings, each cow is located in her own pasture, though some pastures have no cows in them. Each pasture is connected by a path to one or more other pastures (potentially including itself). Sometimes, two (potentially self-same) pastures are connected by more than one path. One or more of the pastures has a path to the barn. Thus, all cows have a path to the barn and they always know the shortest path. Of course, cows can go either direction on a path and they all walk at the same speed.

The pastures are labeled `a'..`z' and `A'..`Y'. One cow is in each pasture labeled with a capital letter. No cow is in a pasture labeled with a lower case letter. The barn's label is `Z'; no cows are in the barn, though.

PROGRAM NAME: comehome
INPUT FORMAT
Line 1:  Integer P (1 <= P <= 10000) the number of paths that interconnect the pastures (and the barn) 
Line 2..P+1:  Space separated, two letters and an integer: the names of the interconnected pastures/barn and the distance between them (1 <= distance <= 1000) 

SAMPLE INPUT (file comehome.in)
5
A d 6
B d 3
C e 9
d Z 8
e Z 3

OUTPUT FORMAT
A single line containing two items: the capital letter name of the pasture of the cow that arrives first back at the barn, the length of the path followed by that cow.
SAMPLE OUTPUT (file comehome.out)
B 11

--------------------------------------------------------------------
本题就是一个拓扑排序,找到到根节点最近的带权节点。
由于最多有26×2=52个节点。因此直接用关联矩阵就可以了。

 

 

 

我的解法:
——————————————————————————————————————————————————————————

/*
ID:yunleis2
PROG:comehome
LANG:C++
*/
#include<iostream>
#include<fstream>
#include<queue>
using namespace std;
int src[26*2][26*2];
int dest[26*2];
int main()
{
	fstream fin("comehome.in",ios::in);
	int p;
	fin>>p;
	for(int i=0;i<52;i++)
	{
		for(int j=0;j<52;j++)
		{
			src[i][j]=-1;
			
			if(i==j)
			{
				src[i][j]=0;
				 
			}
		}
		dest[i]=-1;
	}

	queue<int > q;
	dest['Z'-'A']=0;
	for(int i=0;i<p;i++)
	{
		char a,b;
		int w;
		fin>>a>>b>>w;
		if(a>='a')
			a=a-'a'+'A'+26;
		if(b>='a')
			b=b-'a'+'A'+26;
		if(src[a-'A'][b-'A']==-1||src[a-'A'][b-'A']>w)
		{
			src[a-'A'][b-'A']=w;
			src[b-'A'][a-'A']=w;
		}
	}
 /*
	for(int i=0;i<52;i++)
	{
		for(int j=0;j<52;j++)
		{
			if(i!=j&&src[i][j]!=-1)
				cout<<i<<"\t"<<j<<"\t"<<src[i][j]<<endl;
		}
	}
 */
	q.push('Z'-'A');
	while(!q.empty())
	{
		int t=q.front();
		q.pop();
		for(int i=0;i<52;i++)
		{
			if(src[t][i]!=-1&&(dest[i]==-1||( (src[t][i]+dest[t])<dest[i] )))
			{
				dest[i]=src[t][i]+dest[t];
				q.push(i);
			}
		}

	}
	int ptr=-1;
	for(int i=0;i<26;i++)
	{
		if(dest[i]>0)
		{
			cout<<i<<" "<<dest[i]<<endl;
			if(ptr==-1)
				ptr=i;
			else
				if(dest[i]<dest[ptr])
					ptr=i;
		}
	}
	fstream fout("comehome.out",ios::out);
	fout<<(char)('A'+ptr)<<" "<<dest[ptr]<<endl;
// 	system("pause");
}


 

目录
相关文章
|
1天前
NSSCTF[NISACTF 2022]sign-ezc++
NSSCTF[NISACTF 2022]sign-ezc++
6 0
UVa11958 - Coming Home
UVa11958 - Coming Home
35 0
|
11月前
[USACO 2010 Feb S]Chocolate Eating
[USACO 2010 Feb S]Chocolate Eating
|
11月前
|
算法 C语言 C++
Til the Cows Come Home (USACO 2004 November)(Dijkstra算法)
Til the Cows Come Home (USACO 2004 November)(Dijkstra算法)
38 0
|
11月前
|
BI
[USACO 2009 Dec S]Music Notes
[USACO 2009 Dec S]Music Notes
|
移动开发
洛谷P2639-[USACO09OCT]Bessie‘s Weight Problem G(01背包)
洛谷P2639-[USACO09OCT]Bessie‘s Weight Problem G(01背包)
洛谷P2639-[USACO09OCT]Bessie‘s Weight Problem G(01背包)
洛谷P2871-[USACO07DEC]Charm Bracelet S(01背包模板题)
洛谷P2871-[USACO07DEC]Charm Bracelet S(01背包模板题)
洛谷P2871-[USACO07DEC]Charm Bracelet S(01背包模板题)
洛谷P6207-[USACO06OCT] Cows on Skates G(DFS记录路径)
洛谷P6207-[USACO06OCT] Cows on Skates G(DFS记录路径)
|
消息中间件 数据建模
题解 P1339 【[USACO09OCT]热浪Heat Wave】
题目链接 这道题纯属是一个裸的SPFA;建议先把模板AC之后再做。只需要做一些手脚,就是在加边的时候加一个双向边就好。然后再第一次加点的时候看不懂模板的出门左转度娘。推荐下面一片讲解:友链所以说,直接上代码。
1129 0