CSU-ACM2016暑假集训训练2-DFS(C - Network Saboteur)
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
Description
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
Output file must contain a single integer -- the maximum traffic between the subnetworks.
Output
Sample Input
3
0 50 30
50 0 40
30 40 0
Sample Output
90 解题思路:题目大意:给定一个邻接矩阵,要求将顶点分为A,B两个集合,使得A集合中的所有顶点到B集合所有顶点的距离之和为最大。
首先两个集合的表示用一个一维数组A[],其中A[i]=1,表示节点i在集合A中,为0则在集合B中。
二位数组C[][]存储邻接矩阵,
由于每一个数字有两种选择0和1,结构适用深度优先:从第一个数开始A[0]=1(假设0号顶点一定在集合A中),
对0来说第2个顶点有两种情况,依次类推,结构就出来了。递归出口就是到达第n-1号顶点。
求和用两个for循环加上对存在集合的判定,记录最大值,每次求和结果与最大值比较,如果更大则修改最大值。 收获感想:对深度优先有了更深刻的理解,刚开始编写的时候思路有点混乱,不知道怎么写递归,出口也搞错了,一遍遍的过程中深入理解。
#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std;
int A[],B[],C[][],N,sum,tp=,tp2=;
void bfs(int tp,int l);
int main()
{
memset(A,,);
//memset(B,-1,21); while(scanf("%d",&N)!=EOF){
for(int i=;i<N;i++)
for(int j=;j<N;j++)
scanf("%d",&C[i][j]);
bfs(,);
printf("%d\n",sum);
} return ;
} void bfs(int tp,int l)
{
if(tp==N||tp==-) return;
int s=;
A[tp]=l;
for(int i=;i<N;i++)
{
if(A[i]==)
for(int j=;j<N;j++)
{
if(A[j]==)
s=s+C[i][j]; }
}
if(sum<s) sum=s;
bfs(tp+,);
bfs(tp+,);
}
CSU-ACM2016暑假集训训练2-DFS(C - Network Saboteur)的更多相关文章
- 2016HUAS暑假集训训练题 G - Oil Deposits
Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...
- 2016huasacm暑假集训训练五 H - Coins
题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/H 题意:A有一大堆的硬币,他觉得太重了,想花掉硬币去坐的士:的士司机可以不找零,但 ...
- 2016huasacm暑假集训训练五 J - Max Sum
题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/J 题意:求一段子的连续最大和,只要每个数都大于0 那么就会一直增加,所以只要和0 ...
- 2016huasacm暑假集训训练五 G - 湫湫系列故事——减肥记I
题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/G 这是一个01背包的模板题 AC代码: #include<stdio.h&g ...
- 2016huasacm暑假集训训练五 F - Monkey Banana Problem
题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/F 题意:求至上而下一条路径的所经过的值得和最大值,这题比赛时就出了 但当时看不懂题 ...
- 2016huasacm暑假集训训练五 E - What Is Your Grade?
题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/E 题意:给做出的题目个数,5个的100分,4个的前n/2的同学95,后n/2的90 ...
- 2016huasacm暑假集训训练五 C-Common Subsequence
题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/C 题意:这是一道求字符串的公共子串的最大长度的题目,用dp动态方程即可 if(a[ ...
- 2016huasacm暑假集训训练四 DP_B
题目链接:http://acm.hust.edu.cn/vjudge/contest/125308#problem/M 题意:有N件物品和一个容量为V的背包.第i件物品的费用是体积c[i],价值是w[ ...
- 2016huasacm暑假集训训练四 数论_B
题目链接:http://acm.hust.edu.cn/vjudge/contest/125308#problem/G 题意:求有多少x(1<=x<=n),使得gcd(x,n)>=m ...
随机推荐
- IOS知识小记
iOS开发 小知识点 http://www.cnblogs.com/tangbinblog/archive/2012/07/20/2601324.html Objective-C中的instancet ...
- 如何避免regionServer宕机
为什么regionserver 和Zookeeper的session expired? 可能的原因有 1. 网络不好. 2. Java full GC, 这会block所有的线程.如果时间比较长,也会 ...
- plsql developer的一些使用
.PL/SQL Developer记住登陆密码 在使用PL/SQL Developer时,为了工作方便希望PL/SQL Developer记住登录Oracle的用户名和密码: 设置方法:PL -> ...
- Java算法实例集合
这里是princeton搜集的算法课程Java示例.包括超过了100 Java个算法程序源码.Javadoc和测试数据.点击这里查看.
- android学习日记10--裁剪区域
裁剪区域 裁剪是画布的一个函数,区域可以是矩形和圆形,也可以通过设置 path 或Region来显示自定义区域,通过不同组合,Android几乎可以支持任意现状的裁剪区域.android.graphi ...
- el表达式跟ognl表达式的区别(转)
EL表达式: >>单纯在jsp页面中出现,是在四个作用域中取值,page,request,session,application.>>如果在struts环境中,它除了有在上面的 ...
- emplace_back与push_back的区别
std::vector::emplace_back C++ Containers library std::vector template< class... Args &g ...
- Session Store
Session Store Configuration Session Usage Flash Data Session Drivers Configuration Since HTTP driven ...
- Internationalization
Internationalization If you are building a site for an international audience, you will likely want ...
- felx第二天 ActionScript 基本语法和关键字
flex中使用的注解有两种分别是:// 和/**/ flex使用的关键字如图