poj2377解題報告

來源:互聯網
上載者:User
Bad Cowtractors
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6456   Accepted: 2816

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.

 題目大意:求一最大產生樹

 

 

思路:這個題讓我快崩潰了,WA了很多次,最後看discuss說有重邊。。。改完AC了  瘋了。。。

 

#include<iostream><br />using namespace std;<br />struct t<br />{<br />int s,e,l;<br />}tree[2010],h;<br />int dis[2010][2010];<br />int main()<br />{<br />int n,c,i,j,k,a,b,m;<br />while(cin>>n>>c&&n)<br />{<br />int flag=1;<br />memset(dis,0,sizeof(dis));<br />for(i=1;i<=c;i++)<br />{<br />cin>>a>>b>>j;<br />if(dis[a][b]<j)<br />{dis[a][b]=j;dis[b][a]=j;}<br />}</p><p>for(i=1;i<n;i++)<br />{<br />tree[i].s=1;<br />tree[i].e=i+1;<br />tree[i].l=dis[tree[i].s][tree[i].e];<br />}<br />for(i=1;i<n;i++)<br />{<br />int min=0;<br />for(j=i;j<n;j++)<br />if(tree[j].l>min)<br />{<br />min=tree[j].l;<br />m=j;<br />}<br />if(min==0)<br />{flag=0;break;}<br />else<br />{<br />h=tree[i];tree[i]=tree[m];tree[m]=h;<br />for(j=i+1;j<n;j++)<br />if(tree[j].l<dis[tree[i].e][tree[j].e])<br />{<br />tree[j].s=tree[i].e;<br />tree[j].l=dis[tree[j].s][tree[j].e];<br />}<br />}<br />}<br />if(flag)<br />{<br />int s=0;<br />for(i=1;i<n;i++)<br />s+=tree[i].l;<br />cout<<s<<endl;<br />}<br />else<br />cout<<"-1"<<endl;<br />}<br />return 0;<br />}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.