hxc丶 2021-12-30 12:00 采纳率: 78.3%
浏览 19
已结题

普利姆算法问题,编译通过但是代码无输出

题目描述
The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They're planning to build some highways so that it will be possible to drive between any pair of towns without leaving the highway system.
Flatopian towns are numbered from 1 to N. Each highway connects exactly two towns. All highways follow straight lines. All highways can be used in both directions. Highways can freely cross each other, but a driver can only switch between highways at a town that is located at the end of both highways.
The Flatopian government wants to minimize the length of the longest highway to be built. However, they want to guarantee that every town is highway-reachable from every other town.

输入描述
This problem contains multiple test cases!
The first line of a multiple input is an integer T, then a blank line followed by T input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.
For each case, the first line is an integer N (3 <= N <= 500), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 65536]) between village i and village j.

输出描述
The output format consists of T output blocks. There is a blank line between output blocks.
For each case, you should output a line contains an integer, which is the length of the longest road to be built such that all the villages are connected, and this value is minimum.

我的代码为

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 110   //最多顶点数
#define MAX 0x3f3f3f3f  //模拟无穷大
int map[N][N];  //存储各顶点间的权值
int flag[N];    //标记是否已纳入树
int dis[N];     //已纳入点和其余各点的最小权值
int prim(int n)        //普利姆函数
{
    int i,j,l=0;
    int a[N]={0};
    int now;    //记录新纳入的点
    int min;    //记录新纳入的点到其余已纳入的点的最小权值
    memset(dis, MAX, sizeof(dis));  //初始化dis数组为无穷大
    memset(flag, 0, sizeof(flag));  //初始化flag数组,0表示此点未被纳入
    /*这里随机选取了1号点为初始时被纳入的顶点*/
    for(i = 1; i <= n; i++)
        dis[i] = map[1][i];     //与1号点与其他点的权值存入dis数组
    dis[1] = 0;         //一号点到其本身的权值为0
    flag[1] = 1;        //一号点标记为已纳入

    for(i = 1; i <=n; i++){         //除去初始时随机纳入的点还有n-1个点应被纳入
        now = min = MAX;            //初始为无穷大表示两点间无通路
        for(j = 1; j <= n; j++){    //遍历
            if(flag[j] == 0){
                if(dis[j] < min){   //寻找与已纳入各点权值最小的点
                    now = j;
                    min = dis[j];
                }
            }
        }
        a[++l]=min;
        if(now == MAX)      //若now等于max,则证明所有与初始时纳入的点连通的点已全被纳入
            break;
        flag[now] = 1;     //将找到的点纳入并标记
        for(j = 1; j <= n; j++){
            /*
                遍历比较之前纳入点到未纳入点的权值的最小值
                与刚纳入点到未纳入点的权值
                并用dis[j]存储新的最小值
            */
            if(flag[j] == 0){
                if(dis[j] > map[now][j]){
                    dis[j] = map[now][j];
                }
            }
        }
    }
    int r=0;
    for(int x=0;x<N;x++){
        if (a[x]>=r){
            r=a[x];
        }
    }
    return r;
}
int main()
{
    int i, j, k,T;
    scanf("%d",&T);
    for(int y=1;i<=T;y++){
        int n;
        scanf("%d",&n);
        for(i=1;i<=n;i++){
            for(j=1;j<=n;j++){
                scanf("%d",&map[i][j]);
            }
        }
        int t=prim(n);
        printf("%d\n",t);
        printf("\n");
    }
    
    return 0;
}

求解为何无输出!

  • 写回答

2条回答 默认 最新

  • CSDN专家-sinJack 2021-12-30 12:02
    关注

    你输入了吗

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 9月23日
  • 已采纳回答 9月15日
  • 修改了问题 12月30日
  • 创建了问题 12月30日

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来