【DFS】——poj2531——巧妙最大割
Network Saboteur
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 11017 | Accepted: 5311 |
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).
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 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
题意:把n个点分成两个集合,求两个集合的点间的最大权值和
思路:第一反应居然是土耳其算法,然而20的范围直接暴力搜就好了
1、把这两个集合标记为0和1,先把所有点都放在集合0里。
2、尝试把每个点放到集合1里去,调整集合权值,与id同在集合0里的点,要把权值加上;而在集合1里的点,要把权值减去。
3、权值调整完毕后,和ans比较,如果比ans要大, 调整ans。
4、如果放到集合1中,调整节点后的权值比放在集合0中要大,那么就把这个点放在集合1中,继续枚举下面的点进行DFS。最终是可以把最有状态都枚举出来的。
(最优性剪枝:每个位置之前的点不必继续枚举,因为已经证明那个点不移动会有较大权值)
代码如下:
#include<iostream> #include<string.h> using namespace std; int ans,n; int s[25]; int v[25][25]; void dfs(int id,int sum) { s[id]=1; int temp=sum; for(int i=1;i<=n;i++) { if(s[i]==0) temp+=v[i][id]; else temp-=v[i][id]; } ans=max(ans,temp); for(int i=id+1;i<=n;i++) { if(temp>sum) { dfs(i,temp); s[i]=0; } } } int main() { while(cin>>n) { for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) cin>>v[i][j]; memset(s,0,sizeof(s)); ans=0; dfs(1,0); cout<<ans<<endl; } return 0; }

浙公网安备 33010602011771号