Bessie Come Home(图论)

下面是邻接矩阵

Bessie Come Home
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

(感谢万能的度娘)

这道题是图论的经典题之一,貌似可以使用dijkstra,但是对三重循环深爱的我果断用了floyed。

只要寻找到每两个点之间的最短距离,在枚举一遍到Z的所有路,找到最短的一条路输出。

#include<bits/stdc++.h>
using namespace std;
int d[120][120]={};
int num(char a)
{
	if(a>='A'&&a<='Z')
	 return a-65+1;
	else
	 return a-97+26+1;
}
int main()
{
	int n=56;
	int p;
	cin>>p;
	memset(d,10,sizeof(d));
	for(int i=1;i<=p;i++)
	{
		char x,y;
		cin>>x>>y;
		int t;
		cin>>t;
		d[num(x)][num(y)]=t;
		d[num(y)][num(x)]=t;
	}
	for(int i=1;i<=n;i++)
	 d[i][i]=0;
	for(int k=1;k<=n;k++)
	 for(int i=1;i<=n;i++)
	  for(int j=1;j<=n;j++)
	   d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
	int minnum=0;
	int minn=1000000000;
	for(int i=1;i<=25;i++)
	 if(d[i][26]<minn)
	 {
	 	minn=d[i][26];
	 	minnum=i;
	 }
	 cout<<char(minnum+65-1)<<' ';
	 cout<<minn<<endl;
	 return 0;
} 

但是只有80分,要注意去重

正解如下

#include<bits/stdc++.h>
using namespace std;
int d[120][120]={};
int num(char a)
{
	if(a>='A'&&a<='Z')
	 return a-65+1;
	else
	 return a-97+26+1;
}
int main()
{
	int n=56;
	int p;
	cin>>p;
	memset(d,10,sizeof(d));
	for(int i=1;i<=p;i++)
	{
		char x,y;
		cin>>x>>y;
		int t;
		cin>>t;
		d[num(x)][num(y)]=min(d[num(x)][num(y)],t);
		d[num(y)][num(x)]=min(d[num(x)][num(y)],t);
	}
	for(int i=1;i<=n;i++)
	 d[i][i]=0;
	for(int k=1;k<=n;k++)
	 for(int i=1;i<=n;i++)
	  for(int j=1;j<=n;j++)
	   d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
	int minnum=0;
	int minn=1000000000;
	for(int i=1;i<=25;i++)
	 if(d[i][26]<=minn)
	 {
	 	minn=d[i][26];
	 	minnum=i;
	 }
	 cout<<char(minnum+65-1)<<' ';
	 cout<<minn<<endl;
	 return 0;
} 

猜你喜欢

转载自blog.csdn.net/qq_40681184/article/details/80322941
今日推荐