zoukankan      html  css  js  c++  java
  • poj_3371Connect the Cities(最小生成树)

    Connect the Cities

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 5793    Accepted Submission(s): 1701


    Problem Description
    In 2100, since the sea level rise, most of the cities disappear. Though some survived cities are still connected with others, but most of them become disconnected. The government wants to build some roads to connect all of these cities again, but they don’t want to take too much money.  
     

    Input
    The first line contains the number of test cases.
    Each test case starts with three integers: n, m and k. n (3 <= n <=500) stands for the number of survived cities, m (0 <= m <= 25000) stands for the number of roads you can choose to connect the cities and k (0 <= k <= 100) stands for the number of still connected cities.
    To make it easy, the cities are signed from 1 to n.
    Then follow m lines, each contains three integers p, q and c (0 <= c <= 1000), means it takes c to connect p and q.
    Then follow k lines, each line starts with an integer t (2 <= t <= n) stands for the number of this connected cities. Then t integers follow stands for the id of these cities.
     

    Output
    For each case, output the least money you need to take, if it’s impossible, just output -1.
     

    Sample Input
    1 6 4 3 1 4 2 2 6 1 2 3 5 3 4 33 2 1 2 2 1 3 3 4 5 6
     

    Sample Output
    1

    开始判断sum值,wa了,看题目(0 <= c <= 1000),并不能从sum=0判断的=。=


    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    using namespace std;
    #pragma warning(disable : 4996)
    #define MAX 25005
    typedef struct edge
    {
    	int x, y;
    	int w;
    }edge;
    
    edge e[MAX];
    int father[MAX], ranks[MAX];
    
    bool cmp(edge a,edge b)
    {
    	return a.w < b.w;
    }
    
    void Make_Set(int n)
    {
    	for(int i = 1; i <= n; i++)
    	{
    		father[i] = i;
    		ranks[i] = 0;
    	}
    }
    
    int Find_Set(int x)
    {
    	if(x != father[x])
    		father[x] = Find_Set(father[x]);
    	return father[x];
    }
    
    void Merge_Set(int x, int y)
    {
    	x = Find_Set(x);
    	y = Find_Set(y);
    	if(x == y) return;
    	if(ranks[x] > ranks[y])
    	{
    		father[y] = x;
    	}
    	else if(ranks[x] < ranks[y])
    	{
    		father[x] = y;
    	}
    	else 
    	{
    		ranks[y]++;
    		father[x] = y;
    	}
    }
    
    int main()
    {
    	freopen("in.txt", "r", stdin);
    	int t, i, j, n, m, k, sum, x, y, w, v;
    	scanf("%d", &t);
    	while (t--)
    	{
    		scanf("%d %d %d", &n, &m, &k);
    		Make_Set(n);
    		for(i = 0; i < m; i++)
    		{
    			scanf("%d %d %d", &x, &y, &w);
    			e[i].x = x;
    			e[i].y = y;
    			e[i].w = w;
    		}
    
    		sort(e, e + m, cmp);
    
    		for(i = 1; i <= k; i++)
    		{
    			scanf("%d", &v);
    			if(v == 2)
    			{
    				cin >> x >> y;
    				Merge_Set(x, y);
    			}
    			else
    			{
    				scanf("%d %d", &x, &y);
    				Merge_Set(x, y);
    				x = y;
    				for(j = 2; j < v; j++)
    				{
    					scanf("%d", &y);
    					Merge_Set(x, y);
    					x = y;
    				}
    			}
    		}
    		sum = 0;
    		for(i = 0; i < m; i++)
    		{
    			x = Find_Set(e[i].x);
    			y = Find_Set(e[i].y);
    			if(x != y)
    			{
    				Merge_Set(x, y);
    				sum += e[i].w;
    			}
    		}
    		int count = 0;
    		for(i = 1; i <= n; i++)
    		{
    			if(i == father[i])
    			{
    				count++;
    			}
    		}
    		if(count > 1)
    		{
    			printf("-1\n");
    		}
    		else
    		{
    			printf("%d\n", sum);
    		}
    	}
    	return 0;
    }
    



  • 相关阅读:
    给xpath添加正则表达式匹配函数
    利用window.open实现post方式的参数传递
    利用Http状态码检查网页内容是否更新
    一款很不错的html转xml工具Html Agility Pack
    ASP.NET MVC SiteMap provider的一个bug
    a标记链接相对路径的问题
    下载文件的Restful接口的前端实现
    游戏服务器体系结构
    c++资源之不完全导引 (收藏)
    Apache学习路线
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5835073.html
Copyright © 2011-2022 走看看