USACO 2.4 Bessie Come Home


最短路径问题,数据量很小(最多52个结点),所以用floyd算法就可以了。

#include <iostream>
#include 
<fstream>

using namespace std;

ifstream fin(
"comehome.in");
ofstream fout(
"comehome.out");

#ifdef _DEBUG
#define out cout
#define in cin
#else
#define out fout
#define in fin
#endif

int graph[52][52];

int char2int(char c)
{
    
if(c>='a'&&c<='z')
        
return c-'a';
    
else if(c>='A'&&c<='Z')
        
return c-'A'+26;
}

void floyd()
{
    
for(int k=0;k<52;++k)
        
for(int i=0;i<52;++i)
            
for(int j=0;j<52;++j){
                graph[i][j] 
= min(graph[i][j],graph[i][k]+graph[k][j]);
            }
}

void solve()
{
    
int p;
    
char a,b;
    
int len;

    
for(int i=0;i<52;++i)
        
for(int j=0;j<52;++j)
            graph[i][j] 
= INT_MAX/2;

    
in>>p;

    
while(p--){
       
in>>a>>b>>len;
       
int i = char2int(a);
       
int j = char2int(b);
       graph[i][j] 
= graph[j][i] = min(graph[i][j],len);
    }

    floyd();

    
int res = INT_MAX;
    
int z = char2int('Z');
    
char resi;

    
for(int i=26;i<51;++i){
        
if(graph[z][i]<res){
            resi 
= 'A'+i-26;
            res 
= graph[z][i];
        }
    }

    
out<<resi<<" "<<res<<endl;
}

int main(int argc,char *argv[])
{
    solve(); 
    
return 0;
}


posted on 2009-06-28 17:25 YZY 阅读(1096) 评论(0)  编辑 收藏 引用 所属分类: AlgorithmUSACO图论


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


导航

<2009年6月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

统计

常用链接

留言簿(2)

随笔分类

随笔档案

搜索

积分与排名

最新评论

阅读排行榜