zoukankan      html  css  js  c++  java
  • csuoj 1116: Kingdoms

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1116

    1116: Kingdoms

    Time Limit: 3 Sec  Memory Limit: 64 MB
    Submit: 293  Solved: 82
    [Submit][Status][Web Board]

    Description

    A kingdom has n cities numbered 1 to n, and some bidirectional roads connecting cities. The capital is always city 1.
    After a war, all the roads of the kingdom are destroyed. The king wants to rebuild some of the roads to connect the cities, but unfortunately, the kingdom is running out of money. The total cost of rebuilding roads should not exceed K.
    Given the list of m roads that can be rebuilt (other roads are severely damaged and cannot be rebuilt), the king decided to maximize the total population in the capital and all other cities that are connected (directly or indirectly) with the capital (we call it "accessible population"), can you help him?

    Input

    The first line of input contains a single integer T (T<=20), the number of test cases. 
    Each test case begins with three integers n(4<=n<=16), m(1<=m<=100) and K(1<=K<=100,000). 
    The second line contains n positive integers pi (1<=pi<=10,000), the population of each city. 
    Each of the following m lines contains three positive integers u, v, c (1<=u,v<=n, 1<=c<=1000), representing a destroyed road connecting city u and v, whose rebuilding cost is c. 
    Note that two cities can be directly connected by more than one road, but a road cannot directly connect a city and itself.

    Output

    For each test case, print the maximal accessible population.

    Sample Input

    2
    4 6 6
    500 400 300 200
    1 2 4
    1 3 3
    1 4 2
    4 3 5
    2 4 6
    3 2 7
    4 6 5
    500 400 300 200
    1 2 4
    1 3 3
    1 4 2
    4 3 5
    2 4 6
    3 2 7
    

    Sample Output

    1100
    1000
    

    HINT

     

    Source

    湖南省第八届大学生计算机程序设计竞赛

    分析;

    此题可以先确定1是在点集里,然后暴力枚举其它城市是否要连,对每个枚举的结果求最小生成树,选出符合条件的最优解。

    AC代码:

      1 #include <iostream>
      2 #include<cstdio>
      3 #include<cstring>
      4 using namespace std;
      5 const int maxn=0x3f3f3f3f;
      6 int n,m,k;
      7 int population[20];
      8 int Map[20][20];
      9 bool exits[20];
     10 bool vis[20];
     11 int d[20];
     12 int p;
     13 void init(int n)
     14 {
     15     for(int i=1; i<=n; i++)
     16     {
     17         for(int j=1; j<=n; j++)
     18         {
     19             Map[i][j]=(i!=j?maxn:0);
     20         }
     21     }
     22 }
     23 int prim()
     24 {
     25     int sum=0;
     26     memset(vis,false,sizeof(vis));
     27     for(int i=1; i<=n; i++)
     28     {
     29         d[i]=Map[1][i];
     30     }
     31     vis[1]=true;
     32     for(int i=2; i<=n; i++)
     33     {
     34 
     35         int min=maxn,mini;
     36         for(int j=1; j<=n; j++)
     37         {
     38             if(!vis[j]&&exits[j]&&d[j]<min)
     39             {
     40                 min=d[j];
     41                 mini=j;
     42             }
     43         }
     44         if(min==maxn) break;
     45         sum+=min;
     46         vis[mini]=true;
     47         for(int k=1; k<=n; k++)
     48         {
     49             if(exits[k]&&!vis[k]&&Map[mini][k]<d[k])
     50             {
     51                 d[k]=Map[mini][k];
     52             }
     53         }
     54 
     55 
     56     }
     57     int cnt1=0,cnt2=0;
     58     for(int i=1;i<=n;i++){
     59         cnt1+=exits[i];
     60     }
     61     for(int i=1;i<=n;i++){
     62         cnt2+=vis[i];
     63     }
     64     if(cnt1==cnt2)
     65     return sum;
     66     else return maxn;
     67 
     68 }
     69 
     70 void dfs(int cur)
     71 {
     72     if(cur>n)
     73     {
     74         int pr=prim();
     75         int temp=0;
     76         if(pr<=k)
     77         {
     78             for(int i=1; i<=n; i++)
     79             {
     80                 if(exits[i])
     81                 {
     82                     temp+=population[i];
     83                 }
     84             }
     85             if(temp>p) p=temp;
     86         }
     87         return ;
     88     }
     89     for(int i=0; i<2; i++)
     90     {
     91         exits[cur]=i==1?true:false;
     92         dfs(cur+1);
     93     }
     94 }
     95 int main()
     96 {
     97     int t;
     98     scanf("%d",&t);
     99     while(t--)
    100     {
    101         scanf("%d%d%d",&n,&m,&k);
    102         for(int i=1; i<=n; i++)
    103         {
    104             scanf("%d",&population[i]);
    105         }
    106         init(n);
    107         for(int i=0; i<m; i++)
    108         {
    109             int from,to,c;
    110             scanf("%d%d%d",&from,&to,&c);
    111             if(Map[from][to]>c)
    112             {
    113                 Map[from][to]=Map[to][from]=c;
    114             }
    115         }
    116         p=population[1];
    117         exits[1]=true;
    118         dfs(2);
    119         printf("%d
    ",p);
    120     }
    121     return 0;
    122 }
    View Code
  • 相关阅读:
    angularjs 学习笔记(一)
    iconfont项目成员添加不进去的问题
    服务器webapi集成极光推送学习笔记
    winform closing事件注册
    asp.net core webapi 似乎未安装在 IIS 中承载 .NET Core 项目所需的 AspNetCoreModule。请尝试修复 Visual Studio 以纠正该问题。
    css 行内元素设置宽高
    透过浏览器看HTTP缓存[转载]
    矢量字体图标
    HTML5新特性之WebRTC[转]
    一个前后端分离方案[转载]
  • 原文地址:https://www.cnblogs.com/jeff-wgc/p/4475750.html
Copyright © 2011-2022 走看看