USACO SECTION 2.4 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

 

/*
ID: conicoc1
LANG: C
TASK: comehome
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>

#define INIFINITY 520000

int Pas[123][123];
int P;
int Dist[123];
int Heap[128];
int Flag[123];
int HeapSize=52;
int MinDist=INIFINITY,Min;

void Initial()
{
	int i,j;
	for(i='A';i<='z';i++){
		for(j='A';j<='z';j++){
			Pas[i][j]=INIFINITY;
		}
		Dist[i]=INIFINITY;
	}
}
void PercolateDown(int i)
{
	int Child,temp;
	temp=Heap[i];
	for(;i*2<HeapSize;i=Child){
		Child=2*i;
		if(Child!=HeapSize&&Dist[Heap[Child]]>Dist[Heap[Child+1]])
			Child++;
		if(Dist[temp]>Dist[Heap[Child]])
			Heap[i]=Heap[Child];
		else
			break;	
	}
	Heap[i]=temp;
}

void BuildHeap()
{
	int i;
	for(i=1;i<=26;i++){
		Heap[i]='A'+i-1;
	}	
	for(i=1;i<=26;i++){
		Heap[i+26]='a'+i-1;
	}
	
	for(i=52/2;i>0;i--){
		PercolateDown(i);
	}
}

void DecreaseKey(int i)
{
	int Father,temp;
	temp=Heap[i];
	for(;i/2>0;i=Father){
		Father=i/2;
		if(Dist[temp]<Dist[Heap[Father]])
			Heap[i]=Heap[Father];
		else
			break;
	}
	Heap[i]=temp;
}

int DeleteMin()
{
	int temp;
	if(HeapSize==0)
		return -1;
	temp=Heap[1];
	Heap[1]=Heap[HeapSize];
	Heap[HeapSize--]=temp;
	PercolateDown(1);
	
	return temp;
}

int main()
{
	FILE *fin,*fout;
	int i,j;
	int P,Distance;
	char Pas1,Pas2;
	int Vertex;
	fin=fopen("comehome.in","r");
	fout=fopen("comehome.out","w");
	
	Initial();
	fscanf(fin,"%d",&P);
	for(i=0;i<P;i++){
		fscanf(fin," %c %c %d",&Pas1,&Pas2,&Distance);
		if(Distance<Pas[Pas1][Pas2]){
			Pas[Pas1][Pas2]=Distance;
			Pas[Pas2][Pas1]=Distance;
		}
	}
	//读入数据完毕
	Dist['Z']=0;
	 
	BuildHeap();
	while(Dist[Heap[1]]!=INIFINITY){
		Vertex=DeleteMin();
		if(Vertex==-1)
			break;
		for(i=1;i<=HeapSize;i++){
			if(Dist[Vertex]+Pas[Heap[i]][Vertex]<Dist[Heap[i]]){
				Dist[Heap[i]]=Dist[Vertex]+Pas[Heap[i]][Vertex];
				DecreaseKey(i);
			}	
		}
	}
	
	for(i='A';i<'Z';i++){
		if(MinDist>Dist[i]){
			Min=i;
			MinDist=Dist[i];	
		}
	}
	fprintf(fout,"%c %d\n",Min,MinDist);
	return 0;
}


 这道即使AC了之后也是想哭啊。。

其实知道这题数据范围最多52个点,用Floyd是肯定不会超时的。

为了锻炼一下还是用dijkstra,还是写了个经过堆优化的。。。

尼玛这个堆优化写的纠结啊,因为我开始读入数据的时候,点都是用字母的ASC码表示的,由于‘Z’和‘a'直接还隔了几个字母,然后存入堆里的时候发现了一连串的麻烦,我不知道大牛们这个堆优化到底是怎么写的。。或者是我从来没有写过二叉堆的操作,生疏啊生疏

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值