天天看點

POJ-2377 Bad Cowtractors

​​題目傳送門​​​ Bad Cowtractors

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 16504 Accepted: 6714

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 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.

Source

USACO 2004 December Silver

這個題是最小生成樹的變形題,求最大生成樹,這個也好辦,最小生成樹求的是邊權和最小的一棵樹,那麼我們把每個正邊權取為負邊權,再去求最小生成樹,得到的最小的一棵樹的邊權和,肯定是一個負數,并且是最小的,那麼它的相反數就是最大的正邊權和的一棵樹,就是最大生成樹。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
using namespace std;
typedef pair<int,int>P;
const int maxn=1000+10;
const int INF=0x3f3f3f3f;
struct Edge{
  int to,len;
  Edge(int to_=0,int len_=0)
  {
    to=to_;
    len=len_;
  }
};
vector<Edge>G[maxn];
int dis[maxn];
bool vis[maxn];
int n,m;
void init()
{
  for(int i=1;i<=n;i++)
  G[i].clear();
}
void Add(int from,int to,int len)
{
  G[from].push_back(Edge(to,len));
  G[to].push_back(Edge(from,len));
}
int prime(int s)
{
  fill(dis,dis+n+1,INF);
  dis[s]=0;
  memset(vis,false,sizeof(vis));
  priority_queue<P,vector<P>,greater<P> >que;
  int u,v,i,len,cnt=0,ans=0;
  Edge e;
  P p;
  que.push(P(0,s));
  while(!que.empty())
  {
    p=que.top();
    que.pop();
    u=p.second;
    if(vis[u])
    continue;
    vis[u]=true;
    cnt++;
    ans+=dis[u];
    len=G[u].size();
    for(int i=0;i<len;i++)
    {
      e=G[u][i];
      v=e.to;
      if(!vis[v]&&dis[v]>e.len)
      {
           dis[v]=e.len;
         que.push(P(dis[v],v)); 
      } 
    }
  }
  if(cnt!=n)
  return -1;
  return -ans;
}
int main()
{
  while(scanf("%d%d",&n,&m)==2)
  {
    init();
    for(int i=0;i<m;i++)
    {
       int u,v,len;
       scanf("%d%d%d",&u,&v,&len);
       Add(u,v,-len);
    }
    printf("%d\n",prime(1));
  }
}