Bad Cowtractors
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6904 | Accepted: 3008 |
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".
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.
* 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 8 1 2 3 1 3 7 2 3 10 2 4 4 2 5 8 3 4 6 3 5 2 4 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.
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
题目意思很明了,就是想叫你求一个最大生成树
我的想法和网上流行的算法有点不一样,正所谓非主流就是这个意思。。。
主流的算法是利用克鲁斯卡尔算法,并且在排序的时候按降序排列
而我是把所有的边都存为负权,然后再利用prim求最小生成树、最后答案取负就可以了
注意:数据中有重边,必须要去掉。另外不连通的情况要慎重考虑
我的代码:
#include<stdio.h>
#include<algorithm>
#define inf 99999999
using namespace std;
struct node
{
int v1;
int v2;
int len;
};
node e[1005];
int dis[1005][1005];
void init(int n)
{
int i,j;
for(i=0;i<=n;i++)
for(j=0;j<=n;j++)
dis[i][j]=inf;
}
int prim(int n)
{
int i,j,vx,vy,leng,min,minl;
int res=0;
for(i=1;i<=n-1;i++)
{
e[i].v1=1;
e[i].v2=i+1;
e[i].len=dis[1][i+1];
}
for(i=1;i<=n-1;i++)
{
min=-1;
minl=2*inf;
for(j=i;j<=n-1;j++)
{
if(e[j].len<minl)
{
minl=e[j].len;
min=j;
}
}
if(min==-1)
break;
res=res+minl;
swap(e[i],e[min]);
vx=e[i].v2;
for(j=i+1;j<=n-1;j++)
{
vy=e[j].v2;
leng=dis[vx][vy];
if(leng<e[j].len)
{
e[j].len=leng;
e[j].v1=vx;
}
}
}
return res;
}
int main()
{
int i,a,b,n,m,c;
scanf("%d%d",&n,&m);
init(n);
for(i=0;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
if(dis[a][b]==inf||-c<dis[a][b])
{
dis[a][b]=-c;
dis[b][a]=-c;
}
}
int ans=prim(n);
if(ans>0)
printf("-1/n");
else
printf("%d/n",-ans);
return 0;
}
----------------------------------------------------------------------------------------------------------传说中的分割线。。。