hdu 2122 Ice_cream’s world III(最小生成树))

来源:互联网 发布:windows 设置字符集 编辑:程序博客网 时间:2024/05/16 10:59

Problem Description
ice_cream’s world becomes stronger and stronger; every road is built as undirected. The queen enjoys traveling around her world; the queen’s requirement is like II problem, beautifies the roads, by which there are some ways from every city to the capital. The project’s cost should be as less as better.
 

Input
Every case have two integers N and M (N<=1000, M<=10000) meaning N cities and M roads, the cities numbered 0…N-1, following N lines, each line contain three integers S, T and C, meaning S connected with T have a road will cost C.
 

Output
If Wiskey can’t satisfy the queen’s requirement, you must be output “impossible”, otherwise, print the minimum cost in this project. After every case print one blank.
 

Sample Input
2 10 1 104 0
 

Sample Output
10impossible

就是给你城市的数量,和可以修建的铁路及其长度,如果连通,输出最小的总长,否则输出impossible。

#include <cstdio>#include <iostream>using namespace std;const int Max=10000000;int n,m;int map[1005][1005];int ans;int Prim(int v0){    int sum=0;    int lowcost[1005];    for (int i=0; i<n; i++)    {        lowcost[i]=map[v0][i];    }    lowcost[v0]=-1;    for (int i=0; i<n-1; i++)    {        int min=Max;        int k=-1;        for (int j=0; j<n; j++)        {            if(lowcost[j]!=-1&&min>lowcost[j])            {                min=lowcost[j];                k=j;            }        }        if(k==-1)        {            break;        }        ans++;        sum+=lowcost[k];        lowcost[k]=-1;        for (int j=0; j<n; j++)        {            if(lowcost[j]>map[k][j])            {                lowcost[j]=map[k][j];            }        }    }    return sum;}int main(){    while (scanf("%d%d",&n,&m)!=EOF)    {        ans=0;        for (int i=0; i<n; i++)        {            for (int j=0; j<n; j++)            {                if(i==j)                {                    map[i][j]=0;                }                else                {                    map[i][j]=Max;                }            }        }        int a,b,c;        for (int i=0; i<m; i++)        {            scanf("%d%d%d",&a,&b,&c);            if(map[a][b]>c)  //判断重边取小            {                map[a][b]=c;                map[b][a]=c;            }        }        int sum=Prim(0);        if(ans==n-1)        {            cout<<sum<<endl<<endl;        }        else        {            cout<<"impossible"<<endl<<endl;        }    }    return 0;}

可以开始DFS看图是否联通。

bool visit[1005];void DFS(int v0){    visit[v0]=true;    ans++;    for (int i=0; i<n; i++)    {        if(!visit[i] && map[v0][i]!=Max)        {            DFS(i);        }    }}


(2)Kruscal

#include <cstdio>#include <iostream>#include <algorithm>using namespace std;int n,m;int ans;int sum;int father[1005];struct node{    int a,b;    int d;    bool operator<(const node &t)const    {        return d<t.d;    }};int Find(int x){    if(x!=father[x])    {        father[x]=Find(father[x]);    }    return father[x];}void Union(int x,int y,int d){    x=Find(x);    y=Find(y);    if(x!=y)    {        father[x]=y;        ans++;        sum+=d;    }}int main(){    while (scanf("%d%d",&n,&m)!=EOF)    {        ans=0;        node num[10005];        sum=0;        for (int i=0; i<n; i++)        {            father[i]=i;        }        for (int i=0; i<m; i++)        {            scanf("%d%d%d",&num[i].a,&num[i].b,&num[i].d);        }        sort(num, num+m);   //因为有重边        for (int i=0; i<m; i++)        {            Union(num[i].a, num[i].b,num[i].d);        }        if(ans==n-1)        {            cout<<sum<<endl<<endl;        }        else        {            cout<<"impossible"<<endl<<endl;        }    }    return 0;}


0 0
原创粉丝点击