码迷,mamicode.com
首页 > Web开发 > 详细

POJ 2351 Network Saboteur

时间:2018-05-22 22:18:57      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:就是   ati   pac   mes   fail   def   ESS   esc   car   

Network Saboteur
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 14087   Accepted: 6893

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
题意:
将n个数分为两个集合,同一集合内没有距离,不同集合之间有距离,求分成两个集合的最大的距离。
思路:
这道题我们采用DFS的方法进行求解,我们先考虑最简单情况(也就是DFS递归最底层的东西或者说是出口),
我们可以将多对多的问题转化成多个一
对多的问题,用0,1区分两个集合,默认刚开始所有的数都在0集合中,然后不断向1集合中转移数据。最后求一个最大值,就是我们想要的结果
AC代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#define N 30
#define INF 0x3f3f3f3f
using namespace std;
int mp[N][N];
bool vis[N];
int max1;
int n;
void DFS(int s,int sum){
vis[s]=true;//vis[]=true--在1集合中
for(int i=1;i<=n;i++){
    if(!vis[i])//如果在不同的集合
      sum+=mp[s][i];
    else //在相同的集合
     sum-=mp[s][i];
}
max1=max(max1,sum);
for(int i=s+1;i<=n;i++){//将0中的数据向1转移
    vis[i]=true;
    DFS(i,sum);
    vis[i]=false;//注意回复
}
}
int main(){
while(~scanf("%d",&n)){
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            scanf("%d",&mp[i][j]);
        }
    }
    max1=-INF;
    DFS(0,0);
    printf("%d\n",max1);
    }
return 0;
}
#include<cstdio>
#include<cstring>
#include<iostream>
#define N 30
#define INF 0x3f3f3f3f
using namespace std;
int mp[N][N];
bool vis[N];
int max1;
int n;
void DFS(int s,int sum){
vis[s]=true;//vis[]=true--在1集合中
for(int i=1;i<=n;i++){
    if(!vis[i])//如果在不同的集合
      sum+=mp[s][i];
    else //在相同的集合
     sum-=mp[s][i];
}
max1=max(max1,sum);
for(int i=s+1;i<=n;i++){//将0中的数据向1转移
    vis[i]=true;
    DFS(i,sum);
    vis[i]=false;//注意回复
}
}
int main(){
while(~scanf("%d",&n)){
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            scanf("%d",&mp[i][j]);
        }
    }
    max1=-INF;
    DFS(0,0);
    printf("%d\n",max1);
    }
return 0;
}

 

 

POJ 2351 Network Saboteur

标签:就是   ati   pac   mes   fail   def   ESS   esc   car   

原文地址:https://www.cnblogs.com/by-DSL/p/9073865.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!