zoukankan      html  css  js  c++  java
  • poj2531——dfs递归枚举+小剪枝

    POJ 2531  dfs递归枚举

    Network Saboteur
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 9580   Accepted: 4560

    Description

    A university network is composed of N computers. System administrators gathered information on the traffic between nodes, and carefully divided the network into two subnetworks in order to minimize traffic between parts. 
    A disgruntled computer science student Vasya, after being expelled from the university, decided to have his revenge. He hacked into the university network and decided to reassign computers to maximize the traffic between two subnetworks. 
    Unfortunately, he found that calculating such worst subdivision is one of those problems he, being a student, failed to solve. So he asks you, a more successful CS student, to help him. 
    The traffic data are given in the form of matrix C, where Cij is the amount of data sent between ith and jth nodes (Cij = Cji, Cii = 0). The goal is to divide the network nodes into the two disjointed subsets A and B so as to maximize the sum ∑Cij (i∈A,j∈B).

    Input

    The first line of input contains a number of nodes N (2 <= N <= 20). The following N lines, containing N space-separated integers each, represent the traffic matrix C (0 <= Cij <= 10000). 
    Output file must contain a single integer -- the maximum traffic between the subnetworks. 

    Output

    Output must contain a single integer -- the maximum traffic between the subnetworks.

    Sample Input

    3
    0 50 30
    50 0 40
    30 40 0
    

    Sample Output

    90

    题意:将给定图分成两个子图A,B,使A中结点i到B中结点j的距离和最大,输出最大值
    初看以为是图论题,原来可以直接dfs过。。直接dfs递归枚举顺便剪枝
    //poj2531_dfs递归枚举
    #include<iostream>
    #include<cstdlib>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    
    using namespace std;
    
    const int maxn=30;
    
    int N,C[maxn][maxn];
    int dist[maxn];
    int ans;
    bool vis[maxn];//1表示在集合A,0表示还在集合B
    
    void dfs(int cur,int step,int limit,int sum)
    {
        if(cur+limit-step>N) return;//这个剪枝貌似有点弱,只是从256ms优化到了188ms
        for(int i=1;i<=N;i++){ //从B添加结点cur掉A中
            if(vis[i]) sum-=C[cur][i];//减少到A的边
            else sum+=C[cur][i];//增加到B的边
        }
        if(step==limit){ //到达限制,比较ans,返回
            if(sum>ans) ans=sum;
            return;
        }
        vis[cur]=1;
        for(int i=cur+1;i<=N;i++){
            dfs(i,step+1,limit,sum); //添加下一个
        }
        for(int i=1;i<=N;i++){ //从A中拿回来
            if(vis[i]) sum+=C[cur][i];
            else sum-=C[cur][i];
        }
        vis[cur]=0;//记得还原vis
    }
    
    int main()
    {
        while(cin>>N){
            for(int i=1;i<=N;i++){
                for(int j=1;j<=N;j++){
                    cin>>C[i][j];
                    dist[i]+=C[i][j];
                }
            }
            ans=0;
            for(int i=1;i<=N/2;i++){ //枚举A中的结点个数
                for(int st=1;st<=N;st++){ //递归枚举,st为起点
                    memset(vis,0,sizeof(vis));
                    dfs(st,1,i,0);
                }
            }
            cout<<ans<<endl;
        }
        return 0;
    }
    poj2531_dfs
    没有AC不了的题,只有不努力的ACMER!
  • 相关阅读:
    session中删除数组中的某一个值
    Windows 查看端口占用进程并关闭
    Debian 9 / Debian 10 / Ubuntu 18.04 / Ubuntu 18.10快速开启BBR加速 或 关闭BBR加速
    在ASP.NET Web API 2中使用Owin OAuth 刷新令牌(示例代码)
    在ASP.NET Web API 2中使用Owin基于Token令牌的身份验证
    Web API 2 的操作结果
    WebApi接口安全性 接口权限调用、参数防篡改防止恶意调用
    关于EF中使用Migrations的一些小提示
    Entity Framework 6 多对多增改操作指南
    用MVC5+EF6+WebApi 做一个考试功能(六) 仓储模式 打造EF通用仓储类
  • 原文地址:https://www.cnblogs.com/--560/p/4335087.html
Copyright © 2011-2022 走看看