水题一道,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;
}

  

相关文章:

猜你喜欢
  • 2021-08-27
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案