ZOJ1333 POJ1545 Galactic Import,Floyd算法

只从用了Floyd算法后,我就把Dijkstra算法抛弃了。。。这题要注意的有几点,第一,就是编号可以从任意一个字符开始,所以一般都是开到26*26的矩阵,而且循环必须要循环到26而不是n;第二,就是Floyd的路径更新判断了,可以看我代码。


/*******************************************************************************
 # Author : Neo Fung
 # Email : [email protected]
 # Last modified: 2011-08-16 23:05
 # Filename: ZOJ1333 POJ1545 Galactic Import.cpp
 # Description : 
 ******************************************************************************/
// ZOJ1333 POJ1545 Galactic Import.cpp : Defines the entry point for the console application.
//

// #include "stdafx.h"

#include <fstream>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <string>
#include <math.h>
#include <memory.h>
#include <assert.h>

using namespace std;

#define LIMITS 0.00
#define MAX_NUM 30

int main(void)
{
	// 	ifstream cin("data.txt");
	int n;
	char source;
	double value;
	string str;
	double map[MAX_NUM][MAX_NUM];
	int thrusto[MAX_NUM];
	int index;

	while(cin>>n)
	{
		memset(thrusto,0,sizeof(thrusto));
		for(int i=0;i<MAX_NUM;++i)
		{
			for(int j=0;j<MAX_NUM;++j)
				map[i][j]=LIMITS;
		}

		for(int i=0;i<n;++i)
		{
			cin>>source>>value>>str;
			source -='A';
			if (source=='M'-'A')
			{
				assert(source=='M'-'A');
			}
			for(int j=0;j<str.length();++j)
			{
				map[source][source]=value;
				if(str.at(j)!='*')
				{
					map[source][str.at(j)-'A']=value;
				}
				else
				{
					thrusto[source]=1;
				}
			}
		} 

		for(int i=0;i<MAX_NUM;++i)
		{
			for(int j=0;j<MAX_NUM;++j)
			{
				for(int k=0;k<MAX_NUM;++k)
				{
					//如果j和i,i和k之间有通路,而且原来从j到k的价值,比从j到i,i到k的价值低,则更新
					if(map[j][k]<map[j][i]*0.95 &&map[j][i]>0.0&&map[i][k]>0.0)
					{
						map[j][k]=map[j][i]*map[i][k]*0.95;
					}
				}
			}
		}

		index=0;
		value=0.0;
		for(int j=0;j<MAX_NUM;++j)
		{
			if(thrusto[j])
			{
				for(int i=0;i<MAX_NUM;++i)
				{
					if(value<map[i][j])
					{
						index=i;
						value=map[i][j];
					}
				}
			}
		}
		printf("Import from %c\n",index+'A');
	}
	return 0;
}



你可能感兴趣的:(c,算法,email,import)