POJ 2351 Network Saboteur

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;
}
 

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

时间: 2024-12-11 00:53:44

POJ 2351 Network Saboteur的相关文章

poj 2531 Network Saboteur 解题报告

题目链接:http://poj.org/problem?id=2531 题目意思:将 n 个点分成两个部分A和B(也就是两个子集啦), 使得子集和最大(一定很难理解吧,呵呵).举个例子吧,对于样例,最佳的分法就是把点2分为一个子集,另一个子集理所当然就是1.3了. 2-1 的权值是50,2-3的权值是40,那么最大就是50+40 = 90了. 首先dfs的话,我不太会做啦.看了队长的用了状态压缩来做,一下子觉得好神奇!!!! 可能第一次接触,理解得不是太深刻,先留着吧.就觉得好神奇,好神奇...

poj 2531 -- Network Saboteur

Network Saboteur Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9183   Accepted: 4313 Description A university network is composed of N computers. System administrators gathered information on the traffic between nodes, and carefully di

POJ 2531 Network Saboteur(dfs)

题目代号:POJ 2531 题目链接:http://poj.org/problem?id=2531 Language: Default Network Saboteur Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13290   Accepted: 6428 Description A university network is composed of N computers. System administrator

poj 2531 Network Saboteur (dfs)

Network Saboteur Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9364   Accepted: 4417 Description A university network is composed of N computers. System administrators gathered information on the traffic between nodes, and carefully di

DFS/POJ 2531 Network Saboteur

1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 int n,ans; 5 int a[30][30],f[30]; 6 int cmax(int a,int b){return a>b?a:b;} 7 void dfs(int x,int now) 8 { 9 if (x>n) 10 { 11 ans=cmax(ans,now); 12 return; 13 } 14 int temp=0;

【POJ 2531】Network Saboteur

[POJ 2531]Network Saboteur 图的搜索 剪枝真是门学问..剪好了快的可真不是一倍两倍 刚开始搜的思路有问题 TLE了 后来枚举点暴力搜了一发 两百多ms 由于查找时权值是不断增加的 所以直接找集合间最大权的话不方便设置return点 看disscuss发现有一大牛 建了两个数组 通过所有边权-两集合内部边权(去重) 得到答案 dfs的时候找最小内部边权即可 当前状态权值>当前最小内部边权时直接跳出 两个数组分别寸当前状态两个集合中所含的点 每加一个点分别往两边加 假设要将

poj 1861 Network

Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 13260   Accepted: 5119   Special Judge Description Andrew is working as system administrator and is planning to establish a new network in his company. There will be N hubs in the c

poj Command Network 最小树形图

规定根节点,求一颗生成树使得权值最小,但由于是有向图,所以最小生成树算法失效. 查资料后得知此类问题叫做最小树形图. 解决最小树形图问题的朱刘算法,算法核心基于找 [最小弧集->找环,消环缩点] 的思想,来慢慢构造树形图. 所有的灵魂都在这张图上.0.0 注意缩点后的弧权值的处理 #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algo

ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法

题目连接:ZOJ 1542 POJ 1861 Network 网络 Network Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Andrew is working as system administrator and is planning to establish a new network in his company. There will be N hubs in the company, t