#Sisily# 1002.Highways

5 篇文章 0 订阅
1 篇文章 0 订阅

source:http://soj.sysu.edu.cn/show_problem.php?pid=1002&cid=2388

题意

Time Limit: 1sec Memory Limit:32MB

Description

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.
Input
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.
Output
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.
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.
The output format consists of T output blocks. There is a blank line between output blocks.

Sample Input

Copy sample input to clipboard
1

3
0 990 692
990 0 179
692 179 0

Sample Output

692

闲话

一开始用kruskal算法打,发现超时。
后来参考了下网上答案,是用prim算法。于是,我开始思考,可能这里点太多,边太密集,所以会超时。
后来,自己打了下prim的,发现还是超时。对比了下,发现网上的方法有个很好的地方是用到了动态规划的思想,用一个数组记录已访问点距离为访问点的距离,然后在每次添加新点的时候更新。就避免了每次添加新点,都重新双层循环寻找已添加点最近的点。
然后!!!还是不行!!!最坑的地方来了!!!按照题目说法,两个测例的输出间有一空行。于是我照着打了,但是!!!Sicily报的是“一些答案错误!”
我找了很久没找到问题!最后还是怀疑到格式问题。
果然,只要最后一个测例的输出不必有多一个空行!!!!!

这里附上prim算法的代码和超时的kruskal算法。

prim源代码

#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

int dist[501][501];
bool visited[501];
// 已知点到未知点的最短距离
int low[501];

int main() {
    int T, n;
    scanf("%d", &T);
    while (T--) {
        scanf("%d", &n);
        memset(visited, false, sizeof(bool) * 501);

        // read edges
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < n; j++) {
                scanf("%d", &dist[i][j]);
            }
        }

        // prim
        // 添加第一个点,并找已知点到未知点的最短距离。
        int ans = 0;
        visited[0] = true;
        for(int i = 0; i < n; i++) {
            low[i] = dist[0][i];
        }

        // 已知点到未知点的最短距离
        int minDistTarget;
        // to 是距离已知点的点
        int to, i, j;
        // 添加尽未添加的n-1个点
        for( i = 1; i < n; i++) {
            minDistTarget = 100000;
            for( j = 0; j < n; j++) {
                if(false == visited[j] && low[j] < minDistTarget) {to = j; minDistTarget = low[j];}
            }
            // 所构建最小生成树中的最大边
            if (ans < minDistTarget) {ans = minDistTarget;}
            //标记为true,即添加到已知点
            visited[to] = true;
            // 添加新点后,更新已知点到未知点的最小距离
            for(j = 0; j < n; j++) {
                if(false == visited[j] && dist[to][j] < low[j]) {low[j] = dist[to][j];}
            }
        }

        printf("%d\n", ans);
        // 最后一个测例后,不用再多一行空行
        if (T != 0){printf("\n");}
    }
    return 0;
}

超时的kruskal算法

#include <iostream>
#include <cstring>
#include <stdio.h>
using namespace std;
struct edge {
    int from;
    int to;
    int w;
    edge(int f = 0, int t = 0, int weight = 0):from(f), to(t), w(weight){}
    void operator=(edge e){ from = e.from; to = e.to; w = e.w;}
    bool operator<(edge e){return this->w < e.w;}
};


edge edges[125010];
int edgeCounter;
bool U[501];
int uCounter;

int main() {
    int T, n;
    scanf("%d", &T);
    while (T--) {
        scanf("%d", &n);
        // read edges
        edgeCounter = uCounter = 0;
        int hold;
        memset(U, false, sizeof(bool)*n);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                cin >> hold;
                if (j > i) {
                    edges[edgeCounter].from = i;
                    edges[edgeCounter].to = j;
                    edges[edgeCounter].w = hold;
                    edgeCounter++;
                }
            }
        }

        // sort edges
        edge tmp;
        for (int i = 0; i < edgeCounter - 1; i++) {
            for (int j = edgeCounter-1; j > i; j--) {
                if (edges[j] < edges[j-1]) {
                    tmp = edges[j];
                    edges[j] = edges[j-1];
                    edges[j-1] = tmp;
                }
            }
        }

        // select edges;
        int length = 0;
        int i = 0;
        do {
            tmp = edges[i];
            if (!((U[tmp.from])&&(U[tmp.to]))) {
                length = max(length, tmp.w);
                if (false == U[tmp.from]) {U[tmp.from] = true; uCounter++;}
                if (false == U[tmp.to]) {U[tmp.to] = true; uCounter++;}
            }
            i++;
        } while (uCounter < n);
        printf("%d\n", length);
        if (T!=0){printf("\n");}
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值