zoukankan      html  css  js  c++  java
  • Lightoj1059【最小生成树】

    题意:
    使得所有的位置都能通向一个机场,问最小花费。
    思路:
    最小生成树。
    本来还想标记一下没有出现过的点,其实那个数组已经解决了。==。
    PS:注意路比建造机场还贵?直接造机场得了?
    if there are several answers with minimal cost, choose the one that maximizes the number of airports.

    而且相等也是造机场~

    #include<bits/stdc++.h>
    using namespace std;
    const int N=1e5+10;
    bool vis[N];
    int pre[N],n,m,t;
    struct Node{
    	int x,y,w;
    }q[N];
    bool cmp(Node a,Node b)
    {
    	return a.w<b.w;
    }
    
    int Find(int x)
    {
    	int r=x;
    	while(pre[r]!=r)
    		r=pre[r];
    	int i=x,j;
    	while(pre[i]!=r)
    	{
    		j=pre[i];
    		pre[i]=r;
    		i=j;
    	}
    	return r;
    }
    
    int main()
    {
    	int T,cas=1;
    	scanf("%d",&T);
    	while(T--)
    	{
    		scanf("%d%d%d",&n,&m,&t);
    		for(int i=1;i<=n;i++)
    		{
    			pre[i]=i;
    			vis[i]=false;
    		}
    		for(int i=0;i<m;++i)
    			scanf("%d%d%d",&q[i].x,&q[i].y,&q[i].w);
    		sort(q,q+m,cmp);
    		int ans=0,cnt=0;
    		for(int i=0;i<m&&q[i].w<t;i++)
    		{
    			int x=Find(q[i].x);
    			int y=Find(q[i].y);
    			vis[q[i].x]=vis[q[i].y]=true;
    			if(x!=y)
    			{
    				pre[x]=y;
    				ans+=q[i].w;
    			}
    		}
    		for(int i=1;i<=n;i++)
    			if(pre[i]==i)
    			{
    				ans+=t;
    				cnt++;	
    			}
    		printf("Case %d: %d %d
    ",cas++,ans,cnt);
    	}
    	return 0;
    }
    
    /*
    
    5 3 10
    1 2 1
    3 4 1
    4 5 11
    Case 1: 32 3
    
    */
    
    
    
    
    
    


  • 相关阅读:
    【BZOJ4868】期末考试 [三分][贪心]
    【BZOJ4880】排名的战争 [暴力]
    【BZOJ1449&&2895】球队预算 [费用流]
    【BZOJ1221】【HNOI2001】软件开发 [费用流]
    【BZOJ4837】LRU算法 [模拟]
    Leetcode题解(30)
    Leetcode题解(29)
    Leetcode题解(28)
    Leetcode题解(27)
    Leetcode题解(26)
  • 原文地址:https://www.cnblogs.com/keyboarder-zsq/p/6777381.html
Copyright © 2011-2022 走看看