码迷,mamicode.com
首页 > Web开发 > 详细

poj 1258 Agri-Net poj 2485 Highways

时间:2015-05-31 09:31:15      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:acm   c++   poj   算法   编程   

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

多么单纯的mst!

#include<iostream>
#include<string.h>
using namespace std;
const int INF=103;
int graph[INF][INF];
int f1[INF];
int low[INF];
int res,n;

int prim()
{
    f1[1]=1;
    memset(low,0,sizeof(low));

    for(int i=1; i<=n; i++)
    {
        low[i]=graph[1][i];

    }

    int loc,MIN=0x1f1f1f1f;
    for(int i=1; i<n; i++)
    {
        MIN=0x1f1f1f1f;
        for(int j=1; j<=n; j++)
        {
            if(!f1[j]&&low[j]<MIN)
            {
                loc=j;
                MIN=low[j];
            }
        }
        f1[loc]=1;
        res+=MIN;
        for(int i=1; i<=n; i++)
        {
            if(!f1[i]&&graph[loc][i]<low[i])
            {
                low[i]=graph[loc][i];
            }
        }
    }
    return res;

}
int main()
{
    while( cin>>n)
    {
        res=0;
        memset(graph,0,sizeof(graph));
        memset(f1,0,sizeof(f1));
        for(int i=1; i<=n; i++)
        {
            for(int  j=1; j<=n; j++)
                cin>>graph[i][j];
        }
        cout<<prim()<<endl;
    }
    return 0;
}

http://write.blog.csdn.net/postedit

同一个婊子~没意思!

#include<iostream>
#include<string.h>
using namespace std;
const int INF=103;
int graph[INF][INF];
int f1[INF];
int low[INF];
int res,n;

int prim()
{
    f1[1]=1;
    memset(low,0,sizeof(low));

    for(int i=1; i<=n; i++)
    {
        low[i]=graph[1][i];

    }

    int loc,MIN=0x1f1f1f1f;
    for(int i=1; i<n; i++)
    {
        MIN=0x1f1f1f1f;
        for(int j=1; j<=n; j++)
        {
            if(!f1[j]&&low[j]<MIN)
            {
                loc=j;
                MIN=low[j];
            }
        }
        f1[loc]=1;
        res+=MIN;
        for(int i=1; i<=n; i++)
        {
            if(!f1[i]&&graph[loc][i]<low[i])
            {
                low[i]=graph[loc][i];
            }
        }
    }
    return res;

}
int main()
{
    while( cin>>n)
    {
        res=0;
        memset(graph,0,sizeof(graph));
        memset(f1,0,sizeof(f1));
        for(int i=1; i<=n; i++)
        {
            for(int  j=1; j<=n; j++)
                cin>>graph[i][j];
        }
        cout<<prim()<<endl;
    }
    return 0;
}


 


 

poj 1258 Agri-Net poj 2485 Highways

标签:acm   c++   poj   算法   编程   

原文地址:http://blog.csdn.net/lsgqjh/article/details/46277743

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!