POJ2421-Constructing Roads

http://poj.org/problem?id=2421

#include
#include
#include
#define INF 0x7fffffff
#define N 101
using namespace std;
int map[N][N],low[N],vis[N],n,Q;
int prim()
{
    int i,j,k,min,sum=0;
    memset(vis,0,sizeof(vis));
    for(i=1;i<=n;i++)
       low[i]=map[1][i];
    vis[1]=1;
    for(i=1;i)
    {
        min=INF;
        for(j=1;j<=n;j++)
        if(!vis[j]&&min>low[j])
        {
            min=low[j];
            k=j;
        }
        vis[k]=1;
        sum+=min;
        for(j=1;j<=n;j++)
        {
            if(!vis[j]&&low[j]>map[k][j])
            low[j]=map[k][j];
        }
     }
     return sum;
}
int main(void)
{
    int i,j,x,y;
    while(scanf("%d",&n)!=EOF)
    {
        for(i=1;i<=n;i++)
           for(j=1;j<=n;j++)
           {
                  cin>>map[i][j];
               if(map[i][j]==0) 
                  map[j][i]=map[i][j]=INF;
               else 
                  map[j][i]=map[i][j];
           }
        cin>>Q;
        for(i=1;i<=Q;i++)
        {
            cin>>x>>y;
            map[x][y]=map[y][x]=0;
        }
        cout<endl;
    }
}

转载于:https://www.cnblogs.com/YogurtShen/archive/2012/08/30/2664496.html

你可能感兴趣的:(POJ2421-Constructing Roads)