Bad Cowtractors
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 15864 | Accepted: 6481 |
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
用最小生成树算法prim来做,但在之前要把连接的造价变成负的,所以寻找最大值转换为了寻找最小值,还要注意两个谷仓之间的连接,输入样例可能有多种,要有条件判断,选择造价最大的那个取负作为cost。
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
#define INF 1e9+1
#define MAX_V 1005
int cost[MAX_V][MAX_V];
int mincost[MAX_V];
bool used[MAX_V];
int V,E;
void init()
{
for(int i=0;i<V;i++)
{
for(int j=0;j<V;j++)
{
cost[i][j]=INF;
}
cost[i][i]=0;
}
}
int prim()
{
for(int i=0;i<V;i++)
{
mincost[i]=INF;
used[i]=false;
}
mincost[0]=0;
int res=0;
while(true)
{
int v=-1;
for(int u=0;u<V;u++)
if(!used[u]&&(v==-1||mincost[u]<mincost[v]))v=u;
if(v==-1)break;
used[v]=true;
res+=mincost[v];
for(int u=0;u<V;u++)
mincost[u]=min(mincost[u],cost[v][u]);
}
return res;
}
int main()
{
cin>>V>>E;
init();
int f,t,c;
for(int i=0;i<E;i++)
{
cin>>f>>t>>c;
if(cost[f-1][t-1]==INF||(cost[f-1][t-1]>-c)) //这个条件很重要,两个谷仓之间可能有多种连接方式,找到-c最小的一种
{
cost[f-1][t-1]=-c;
cost[t-1][f-1]=-c;
}
}
int ans=prim();
if(ans>0)cout<<-1<<endl;
else cout<<-ans<<endl;
}