poj 2421 Constructing Roads MST

简介:

  一开始看错题了,是有已建好的路,看成必须要建的路了,prim很水


/*
author:jxy
lang:C/C++
university:China,Xidian University
**If you need to reprint,please indicate the source**
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#define INF 1E9
using namespace std;
int d[101][101];
int vis[101];
int dis[101];
int K;
int main()
{
    int n,m;
    while(~scanf("%d",&n)&&n)
    {
        memset(vis,0,sizeof(vis));
        memset(dis,127,sizeof(dis));
        int i,j,a,b;
        K=1;
        for(i=0;i<n;i++)
         for(j=0;j<n;j++)
          scanf("%d",&d[i][j]);
        scanf("%d",&m);
        int ans=0,Min,t;
        for(i=0;i<m;i++)
        {
            scanf("%d%d",&a,&b);
            a--,b--;
            d[a][b]=d[b][a]=0;
        }
        int now=0;
        for(i=0;i<n;i++)
        {
            Min=INF;
            for(j=0;j<n;j++)
            {
                if(dis[j]>=Min||vis[j])continue;
                Min=dis[j];now=j;
            }
            vis[now]=1;
            if(now)ans+=Min;
            for(j=0;j<n;j++)
            {
                if(vis[j])continue;
                dis[j]=min(dis[j],d[now][j]);
            }

        }
        printf("%d\n",ans);
    }
}


目录
相关文章
|
8月前
Jungle Roads(最小生成树)
Jungle Roads(最小生成树)
Constructing Roads(kruskal)
Constructing Roads(kruskal)
73 0
CF191C——Fools and Roads(树上边差分+LCA)
CF191C——Fools and Roads(树上边差分+LCA)
136 0
|
机器学习/深度学习 人工智能 BI
codeforces-1242-B 0-1 MST
B. 0-1 MST time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output Ujan has a lot of useless stuff in his drawers, a considerable part of which are his math notebooks: it is time to sort them out.
172 0
codeforces-1242-B 0-1 MST
[HDU 4738] Caocao‘s Bridges | Tarjan 求割边
Problem Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wouldn’t give up. Caocao’s army still was not good at water battles, so he came up with another idea. He built many islands in the Changjiang river,
148 0