zoukankan      html  css  js  c++  java
  • Connect the Cities--hdoj

                                                Connect the Cities

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
    Total Submission(s) : 7   Accepted Submission(s) : 5
    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
     

    Author
    dandelion
     

    Source
    HDOJ Monthly Contest – 2010.04.04



    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    #define INF 0xfffffff
    int map[505][505],mark[505],num[505];
    int main()
    {
    	int t;
    	scanf("%d",&t);
    	while(t--)
    	{
    		int i,j,m,n,k,a,b,c;
    		scanf("%d%d%d",&n,&m,&k);
    		for(i=0;i<=n;i++)
    		for(j=0;j<=n;j++)
    		map[i][j]=map[j][i]=INF;
    		for(i=0;i<m;i++)
    		{
    			scanf("%d%d%d",&a,&b,&c);
    			if(c<map[a][b])
    			map[a][b]=map[b][a]=c;
    		}
    		for(i=0;i<k;i++)
    		{
    			scanf("%d",&a);
    			for(j=0;j<a;j++)
    			scanf("%d",&num[j]);
    			for(j=0;j<a;j++)
    			for(int jj=j+1;jj<a;jj++)
    			{
    				map[num[j]][num[jj]]=map[num[jj]][num[j]]=0;
    			}
    		}
    		int sum=0,flog;
    		memset(mark,0,sizeof(mark));
    		for(i=2;i<=n;i++)
    		{
    			int min=INF;
    			flog=-1;
    			for(j=2;j<=n;j++)
    			{
    				if(!mark[j]&&map[1][j]<min)
    				{
    					flog=j;
    					min=map[1][j];
    				}
    			}
    			if(flog==-1) break;
    			mark[flog]=1;
    			sum+=map[1][flog];
    			for(j=2;j<=n;j++)
    			{
    				if(!mark[j]&&map[1][j]>map[flog][j])
    				map[1][j]=map[flog][j];
    			}
    		}
    		if(i>n)
    		printf("%d
    ",sum);
    		else printf("-1
    ");
    	}
    	return 0;
    }


  • 相关阅读:
    Dynamic Method Binding in Delphi 动态方法绑定
    Server Memory Server Configuration Options 服务器内存服务配置选项
    最大化系统并发连接数.Windows.reg
    js一行代码解决各种IE兼容问题
    [原创]如果软件在网络磁盘中或移动磁盘中运行时需要解决 exception C0000006 异常问题
    用 ghostscript 转化PDF文件为图片 的参数设置
    GhostScript应用一例:使用GhostScript强行修改加密PDF
    Win7 Win8 Win10取不到机器码的处理办法
    WCAG
    页面被iframe与无刷新更换url方法
  • 原文地址:https://www.cnblogs.com/playboy307/p/5273839.html
Copyright © 2011-2022 走看看