博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj2531——dfs递归枚举+小剪枝
阅读量:5334 次
发布时间:2019-06-15

本文共 2611 字,大约阅读时间需要 8 分钟。

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

30 50 3050 0 4030 40 0

Sample Output

90 题意:将给定图分成两个子图A,B,使A中结点i到B中结点j的距离和最大,输出最大值 初看以为是图论题,原来可以直接dfs过。。直接dfs递归枚举顺便剪枝
//poj2531_dfs递归枚举#include
#include
#include
#include
#include
#include
using namespace std;const int maxn=30;int N,C[maxn][maxn];int dist[maxn];int ans;bool vis[maxn];//1表示在集合A,0表示还在集合Bvoid 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<
<
poj2531_dfs

 

转载于:https://www.cnblogs.com/--560/p/4335087.html

你可能感兴趣的文章
【理财】关于理财的网站
查看>>
Ubunt中文乱码
查看>>
《当幸福来敲门》读后
查看>>
【转】系统无法进入睡眠模式解决办法
查看>>
省市县,循环组装,整合大数组
查看>>
stm32中字节对齐问题(__align(n),__packed用法)
查看>>
like tp
查看>>
posix多线程有感--线程高级编程(线程属性函数总结)(代码)
查看>>
spring-使用MyEcilpse创建demo
查看>>
DCDC(4.5V to 23V -3.3V)
查看>>
kettle导数到user_用于left join_20160928
查看>>
activity 保存数据
查看>>
typescript深copy和浅copy
查看>>
linux下的静态库与动态库详解
查看>>
hbuilder调底层运用,多张图片上传
查看>>
较快的maven的settings.xml文件
查看>>
Git之初体验 持续更新
查看>>
随手练——HDU 5015 矩阵快速幂
查看>>
Maven之setting.xml配置文件详解
查看>>
SDK目录结构
查看>>