POJ2377

题目链接:http://poj.org/problem?id=2377

解题思路:

  Warning ! 注意考虑重边 !

  其实就是求最大生成树,没什么好说的,就上面那个坑。

AC代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 const int maxn=1005,inf=0x7fffffff;
 5 int cost[maxn][maxn];
 6 int d[maxn];
 7 int vis[maxn];
 8 int main()
 9 {
10     int N,M;
11     scanf("%d%d",&N,&M);
12     int a,b,c;
13     for(int i=1;i<=N;i++)
14         for(int j=1;j<=i;j++){
15             if(i==j)    cost[i][j]=0;
16             else    cost[i][j]=cost[j][i]=-inf;
17         }
18     for(int i=0;i<M;i++){
19         scanf("%d%d%d",&a,&b,&c);
20         if(cost[a][b]>-inf){
21             if(cost[a][b]<c)    cost[a][b]=cost[b][a]=c;
22         }
23         else    cost[a][b]=cost[b][a]=c;
24     }
25     for(int i=1;i<=N;i++){
26         d[i]=-inf;
27         vis[i]=0;
28     }
29     d[1]=0;
30     long long res=0;
31     while(1){
32         int v=-1;
33         for(int u=1;u<=N;u++){
34             if(!vis[u]&&(v==-1||d[u]>d[v])) v=u;
35         }
36         if(v==-1)   break;
37         vis[v]=1;
38         res+=d[v];
39         for(int u=1;u<=N;u++){
40             d[u]=max(d[u],cost[v][u]);
41         }
42     }
43     if(res<0)   printf("-1\n");
44     else    printf("%lld\n",res);
45     return 0;
46 }
时间: 2024-10-10 22:21:38

POJ2377的相关文章

POJ2377 Bad Cowtractors【Kruskal】【求最大生成树】

Bad Cowtractors Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10933 Accepted: 4614 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

poj2377 Bad Cowtractors

思路: 最大生成树. 实现: 1 #include <iostream> 2 #include <cstdio> 3 #include <vector> 4 #include <algorithm> 5 #include <cmath> 6 using namespace std; 7 8 struct edge 9 { 10 int a, b, cost; 11 }; 12 edge es[20005]; 13 int ran[1005]; 1

POJ2377最大生成树

题意:求最大生成树 分析:把边权值变成负值,最后取绝对值,注意最后的判断,如果生成树的边的数目小于(顶点数-1)则表示不能构成生成树 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <string> 5 #include <vector> 6 #include <algorithm> 7 #include <set> 8 #

[POJ2377]Bad Cowtractors(最大生成树,Kruskal)

题目链接:http://poj.org/problem?id=2377 于是就找了一道最大生成树的AC了一下,注意不连通的情况啊,WA了一次. 1 /* 2 ━━━━━┒ギリギリ♂ eye! 3 ┓┏┓┏┓┃キリキリ♂ mind! 4 ┛┗┛┗┛┃\○/ 5 ┓┏┓┏┓┃ / 6 ┛┗┛┗┛┃ノ) 7 ┓┏┓┏┓┃ 8 ┛┗┛┗┛┃ 9 ┓┏┓┏┓┃ 10 ┛┗┛┗┛┃ 11 ┓┏┓┏┓┃ 12 ┛┗┛┗┛┃ 13 ┓┏┓┏┓┃ 14 ┃┃┃┃┃┃ 15 ┻┻┻┻┻┻ 16 */ 17 #inc

POJ2377 Bad Cowtractors 【最大生成树】

Bad Cowtractors Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10885   Accepted: 4586 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

poj图论解题报告索引

最短路径: poj1125 - Stockbroker Grapevine(多源最短路径,floyd) poj1502 - MPI Maelstrom(单源最短路径,dijkstra,bellman-ford,spfa) poj1511 - Invitation Cards(单源来回最短路径,spfa邻接表) poj1797 - Heavy Transportation(最大边,最短路变形,dijkstra,spfa,bellman-ford) poj2240 - Arbitrage(汇率问题,

CodeForces - 508D Tanya and Password(欧拉通路)

Description While dad was at work, a little girl Tanya decided to play with dad's password to his secret database. Dad's password is a string consisting of n + 2 characters. She has written all the possible n three-letter continuous substrings of the

POJ - 2377 - Bad Cowtractors (最小生成树)

题目链接:https://vjudge.net/problem/POJ-2377#author=tsacm123 题目大意:就是让你算出n个谷仓之间的最大生成树,然后把各条边的值累加起来 #include<set> #include<map> #include<stack> #include<queue> #include<cmath> #include<cstdio> #include<cctype> #include&