zoukankan      html  css  js  c++  java
  • UVa10859 放置街灯

    Placing Lampposts

    As a part of the mission �Beautification of Dhaka City�, the government has decided to replace all the old lampposts with new expensive ones. Since the new ones are quite expensive and the budget is not up to the requirement, the government has decided to buy the minimum number of lampposts required to light the whole city.

    Dhaka city can be modeled as an undirected graph with no cycles, multi-edges or loops. There are several roads and junctions. A lamppost can only be placed on junctions. These lampposts can emit light in all the directions, and that means a lamppost that is placed in a junction will light all the roads leading away from it.

    The �Dhaka City Corporation� has given you the road map of Dhaka city. You are hired to find the minimum number of lampposts that will be required to light the whole city. These lampposts can then be placed on the required junctions to provide the service. There could be many combinations of placing these lampposts that will cover all the roads. In that case, you have to place them in such a way that the number of roads receiving light from two lampposts is maximized.

    Input

    There will be several cases in the input file. The first line of input will contain an integer T(T<=30) that will determine the number of test cases. Each case will start with two integers N(N<=1000) and M( M<N) that will indicate the number of junctions and roads respectively. The junctions are numbered from 0 to N-1. Each of the next M lines will contain two integers a and b, which implies there is a road from junction a to b,
    ( 0<= a,b < N ) and a != b. There is a blank line separating two consecutive input sets.

    Output

    For each line of input, there will be one line of output. Each output line will contain 3 integers, with one space separating two consecutive numbers. The first of these integers will indicate the minimum number of lampposts required to light the whole city. The second integer will be the number of roads that are receiving lights from two lampposts and the third integer will be the number of roads that are receiving light from only one lamppost.

    Sample Input

    2
    4 3
    0 1
    1 2
    2 3

    5 4
    0 1
    0 2
    0 3
    0 4

    Sample Output

    2 1 2
    1 0 4


    题目大意:给定一个无向无环图,要求在点上放灯,如果某一点上放了等,则可以照亮与它相通的边,现在要求放尽量少得等,使得所有边都被照亮,并且输出灯数,被照亮两次的边数(即边的两个端点均放置灯),被照亮一次的边。 如果等数一样的话,按照被照亮一次边越大的方案。

    解题思路:

    优化目标有两个,使得灯数a尽量少,使得被两个灯的边数b尽量多

    在这里可以转化一下,就是恰被一盏灯照亮的边数c尽量少

    <IMPORTANT>当我们同时需要优化两个变量a,c,要求首先满足a最小的情况下,使得c尽量小

    我们可以引入一个值x=Ma+c,在这里的M是一个很大的值,是一个比c的理论最大值和a的最小理论值之差还要大的值

    为什么?因为如果两个a不同的,无论c相差多少,仍然是a起决定性的作用

    则我们的目标就是使x的值尽量小,那么x/M的整数部分就是灯数,x%M就是只被一个灯照亮的,m-x%M就是被两个灯照亮的

    我们令dp[i][j]表示在i节点的x的最小值(j=1表示父亲节点放灯,0则反之)

    那么对i来说,有两个决策

    如果在i放灯

    那么dp[i][j] = sum(dp[k][1])+M,k是i的子节点。如果j=0且i不是根,还要+1,因为i和他的父亲节点这条边只被一个灯照亮

    如果在i不放灯,必须j=1(不然边就无法照亮)

    那么dp[i][j] = sum(dp[k][0]),如果i不是根,还要加1,因为i和他的父亲节点这条边只被一个灯照亮

    通过边DFS边DP的思路就可以把结果求出来

    另外图可能有多个连通分量

    代码如下:

    #include<iostream> 
    #include<cstdio>
    #include<vector>
    #include<cstring>
    #define Size 1005
    #define M 2000
    using namespace std;
    
    int d[Size][2];
    bool vis[Size][2];
    vector<int> edge[Size];
    int n,m;
    
    
    int dp(int i,int j,int f){
        if(vis[i][j]==true)return d[i][j];
        vis[i][j]=true;
        
        int k=edge[i].size();
        
        //yes
        int& ans=d[i][j];
        ans=M;
        for(int ch=0;ch<k;ch++){
            if(edge[i][ch]!=f){
                ans+=dp(edge[i][ch],1,i);
            }
        }
        if(j==0&&f!=-1)ans++;
        
        //no
        if(j==1 || f==-1){
                int ans2=0;
                for(int ch=0;ch<k;ch++){
                if(edge[i][ch]!=f){
                    ans2+=dp(edge[i][ch],0,i);
                }
            }
            if(f!=-1)ans2++;
            
            ans=min(ans,ans2); 
        }
        
        return ans;
    }
    
    int main(){
        freopen("30.in","r",stdin); 
        
        int T; cin>>T;
        while(T--){
            for(int i=0;i<=n;i++)edge[i].clear();
            memset(vis,false,sizeof(vis));
            
            cin>>n>>m;
            int a,b;
            for(int i=0;i<m;i++){
                cin>>a>>b;
                edge[a].push_back(b);
                edge[b].push_back(a);
            }
            
            int ans=0;
            for(int i=0;i<n;i++){
                if(vis[i][0]==false){
                    ans+=dp(i,0,-1);//-1表示无父节点 即 i为根节点 
                }
            }
            
            cout<<ans/M<<' '
            
            <<m-ans%M<<' '
            <<ans%M<<endl; 
        }
        
        fclose(stdin); 
        return 0; 
    }
  • 相关阅读:
    [调参]batch_size的选择
    [调参]CV炼丹技巧/经验
    [Pytorch]Pytorch加载预训练模型(转)
    [PyTorch]论文pytorch复现中遇到的BUG
    [Opencv]图像的梯度与边缘检测(转)
    freemodbus移植、实例及其测试方法
    eclipse的C/C++开发搭建
    ROS安装
    U-boot移植
    QT开发实战一:图片显示
  • 原文地址:https://www.cnblogs.com/FuTaimeng/p/5424181.html
Copyright © 2011-2022 走看看