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 ...
随机推荐
- ASP.Net Core-TagHelpers
当我们新建了一个.Net Core类型的Project时,我们会看到页面上有类似于这样的代码: 当我们运行项目,查看源代码会发现,浏览器中的就是Html代: 那么,为什么我们在页面写的代码会转化为ht ...
- mysql 源码--xpchild
http://www.cnblogs.com/xpchild/p/3825309.html
- vmware mac虚拟机 停在启动界面
前言: 关于vmware安装mac 10.8 可以从参看:http://www.cnblogs.com/zyf2013/p/3888242.html 安装完成以后重新启动,卡在了白苹果启动画面处. 或 ...
- Linux下各种常见环境变量的配置
Linux系统下各种环境变量都通过修改/etc/profile文件来实现.由于是系统文件,修改此文件需要root权限.因此实现以下功能都需要用户拥有root权限. 另:不要轻易修改profile文 ...
- javaScript面向对象基础
最近学习了js的面向对象,为了能让自己更好的理解,这一篇博客就当作是加深自己学习印象的总结(可能会有很多不足,欢迎指正). js通过函数来创建对象,而且js本身也是一种对象,那么什么又是对象呢,对象包 ...
- failed to obtain a cell from its dataSource
stroyboard没有绑定标示 没有注册标示 cell为空时没有创建
- Android_Gallery
xml布局 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:to ...
- poj 1182 食物链(关系并查集)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 62824 Accepted: 18432 Description ...
- html同一个页面多个倒计时
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head ...
- List中的get(i)
List中的get(i)方法是获取List中的第i个对象吗 是第i+1个对象.List是从0开始的 List是有序的可重复的集合接口