• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
mengxm
博客园    首页    新随笔    联系   管理    订阅  订阅

poj1502

水题一道,dijkstra过,就是题意要理解,它只问从第一个人开始,而不是说任选(图中心问题)

#include <stdio.h>
#include <string.h>
#define NUM 101
#define INF (1<<29)
#define MAX(a,b) ((a)>(b)?(a):(b))
int num,map[NUM][NUM],cost[NUM],max;
char c[NUM];
bool v[NUM];
int Read()
{
    scanf("%s",c);
    if(c[0]=='x')
        return INF;
    else
    {
        int tmp=0,cnt=strlen(c);
        for(int i=0;i<cnt;++i)
            tmp=tmp*10+c[i]-'0';
        return tmp;
    }
}
void dij()
{
    memset(v,false,sizeof(v));
    for(int i=0;i<num;cost[i++]=INF);
    cost[0]=0;max=-1;
    int tmp=0;
    for(int id;tmp!=INF;)
    {
        tmp=INF;
        for(int j=0;j<num;++j)
            if(!v[j]&&tmp>cost[j])
                tmp=cost[j],id=j;
        if(tmp!=INF)
        {
            v[id]=true;
            for(int i=0;i<num;++i)
                if(!v[i]&&cost[id]+map[id][i]<cost[i])
                    cost[i]=cost[id]+map[id][i];
            max=MAX(max,cost[id]);
        }
    }
}
int main()
{
    while(scanf("%d",&num)==1)
    {
        map[0][0]=0;
        for(int i=1;i<num;++i)
        {
            map[i][i]=0;
            for(int j=0;j<i;++j)
                map[j][i]=map[i][j]=Read();
        }
        dij();
        printf("%d\n",max);
    }
    return 0;
}

  

posted @ 2012-02-09 23:34  mengxm  阅读(358)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3