POJ 2377 Bad Cowtractors 最大產生樹

來源:互聯網
上載者:User
點擊開啟連結
Bad Cowtractors
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9681   Accepted: 4109

Description Bessie has been hired to build a cheap internet network among Farmer John's N (2 <= N <= 1,000) barns that are conveniently numbered 1..N. FJ has already done some surveying, and found M (1 <= M <= 20,000) possible connection routes between pairs of barns. Each possible connection route has an associated cost C (1 <= C <= 100,000). Farmer John wants to spend the least amount on connecting the network; he doesn't even want to pay Bessie. 

Realizing Farmer John will not pay her, Bessie decides to do the worst job possible. She must decide on a set of connections to install so that (i) the total cost of these connections is as large as possible, (ii) all the barns are connected together (so that it is possible to reach any barn from any other barn via a path of installed connections), and (iii) so that there are no cycles among the connections (which Farmer John would easily be able to detect). Conditions (ii) and (iii) ensure that the final set of connections will look like a "tree".

Input * Line 1: Two space-separated integers: N and M 

* Lines 2..M+1: Each line contains three space-separated integers A, B, and C that describe a connection route between barns A and B of cost C.

Output * Line 1: A single integer, containing the price of the most expensive tree connecting all the barns. If it is not possible to connect all the barns, output -1.

Sample Input

5 81 2 31 3 72 3 102 4 42 5 83 4 63 5 24 5 17

Sample Output

42

Hint OUTPUT DETAILS: 

The most expensive tree has cost 17 + 8 + 10 + 7 = 42. It uses the following connections: 4 to 5, 2 to 5, 2 to 3, and 1 to 3.

Source USACO 2004 December Silver
給你n個節點,m條邊讓你求最大產生樹,如果沒有輸出-1. 最近敲java敲的都不會c了。 Prime演算法:

#include<stdio.h>#include<string.h>#define M 1007#define inf 0x3f3f3fint map[M][M],dis[M],vis[M];int n,m;void prime(){    int count=0,pos,min,i;    memset(vis,0,sizeof(vis));    for(int i=1;i<=n;i++)        dis[i]=map[1][i];    vis[1]=1;    for(i=1;i<=n;i++)    {        min=-inf;        pos=-1;        for(int j=1;j<=n;j++)            if(!vis[j]&&min<dis[j])            {                pos=j;                min=dis[j];            }        if(min==-inf)            break;        vis[pos]=1;        count+=min;        for(int j=1;j<=n;j++)            if(!vis[j]&&dis[j]<map[j][pos])                dis[j]=map[j][pos];    }    //printf("%d\n",i);    if(i!=n)        printf("-1\n");    else printf("%d\n",count);}int main(){    while(scanf("%d%d",&n,&m)!=EOF)    {        for(int i=0;i<=n;i++)            for(int j=0;j<=n;j++)                map[i][j]=-inf;        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]=map[b][a]=c;        }        prime();    }    return 0;}


Kruksal演算法:
#include<stdio.h>#include<algorithm>#define M 1007#define inf 0x3f3f3fusing namespace std;int pre[M];int n,m;struct E{    int u,v,pos;}edg[M*20];void init(){    for(int i=0;i<=n;i++)        pre[i]=i;}int cmp(E a,E b){    return a.pos>b.pos;}int find(int x){    while(x!=pre[x])        x=pre[x];    return x;}void unio(int a,int b){    int x=find(a);    int y=find(b);    if(x!=y)pre[y]=x;}int main(){    while(scanf("%d%d",&n,&m)!=EOF)    {        init();        for(int i=0;i<m;i++)            scanf("%d%d%d",&edg[i].u,&edg[i].v,&edg[i].pos);        sort(edg,edg+m,cmp);        int sum=0,k=0;        for(int i=0;i<m;i++)            if(find(edg[i].u)!=find(edg[i].v))            {                unio(edg[i].u,edg[i].v);                sum+=edg[i].pos;            }        for(int i=1;i<=n;i++)        {            pre[i]=find(i);            if(pre[i]==i)                k++;        }        if(k>1)printf("-1\n");        else printf("%d\n",sum);    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.