UVa 10147 Highways(最小生成树,水)

这道题,wa了,原因是没有需要建路的时候,输出一行提示,我忽视了

水题,唯一有点特别的是需要输出新建的边,那么就要建一棵前驱树,用数组表示

代码:

#include <cstdio>
#include <cstring>
#include <cmath>
const int N = 800;
const double INF = 10000000000;
int T, n, m, num, p[N];
double map[N][N], d[N], x[N], y[N];
struct highway{
    int u, v;
}way[N];

void prim() {
    for ( int i = 0; i <= n; ++i ) d[i] = INF, p[i] = -1;
    double mi;
    d[1] = 0;
    bool vis[N];
    int v;
    memset(vis, 0, sizeof(vis));
    for ( int u = 0; u < n; ++u ) {
        mi = INF;
        for ( int i = 1; i <= n; ++i ) if ( !vis[i] && d[i] < mi ) {
            v = i, mi = d[i];
        }
        vis[v] = true;
        if ( mi > 0 ) {
            printf("%d %d\n", v, p[v]);
            num++;
        }
        for ( int i = 1; i <= n; ++i ) if ( !vis[i] && d[i] - map[v][i] > 0.0 ) {
            d[i] = map[v][i];
            p[i] = v;
            //printf("%d %d\n", i, p[i] );
        }
    }
}


int main()
{
    scanf("%d", &T);
    while ( T-- ) {
        scanf("%d", &n);
        for ( int i = 1; i <= n; ++i ) scanf("%lf%lf", &x[i], &y[i]);
        for ( int i = 1; i <= n; ++i ) 
            for ( int j = i+1; j <= n; ++j ){ 
                double tmp = (x[i]-x[j])*(x[i]-x[j]) + (y[i]-y[j])*(y[i]-y[j]);
                map[j][i] = map[i][j] = tmp;
            }
        scanf("%d", &m);
        while ( m-- ) {
            int s,t;
            scanf("%d%d", &s, &t);
            map[s][t] = map[t][s] = 0;
        }
        num = 0;
        prim();
        if ( num == 0 ) printf("No new highways need\n");
        if ( T > 0 ) printf("\n");
    }
}


你可能感兴趣的:(UVa 10147 Highways(最小生成树,水))