CSU-ACM2016暑假集训训练2-DFS(C - Network Saboteur)

C - Network Saboteur

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

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

解题思路:题目大意:给定一个邻接矩阵,要求将顶点分为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[21],B[21],C[21][21],N,sum,tp=0,tp2=0;
void bfs(int tp,int l);
int main()
{
    memset(A,0,21);
    //memset(B,-1,21);

    while(scanf("%d",&N)!=EOF){
    for(int i=0;i<N;i++)
    for(int j=0;j<N;j++)
    scanf("%d",&C[i][j]);
    bfs(0,1);
    printf("%d\n",sum);
    }

    return 0;
}

void bfs(int tp,int l)
{
    if(tp==N||tp==-1) return;
    int s=0;
    A[tp]=l;
    for(int i=0;i<N;i++)
    {
        if(A[i]==1)
        for(int j=0;j<N;j++)
        {
            if(A[j]==0)
            s=s+C[i][j];

        }
    }
    if(sum<s) sum=s;
    bfs(tp+1,1);
    bfs(tp+1,0);
}
时间: 2024-08-24 15:55:37

CSU-ACM2016暑假集训训练2-DFS(C - Network Saboteur)的相关文章

2016HUAS暑假集训训练题 G - Oil Deposits

Description The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It

2016HUAS暑假集训训练2 A - Is It A Tree?

本题本是一道并查集的题,但出的数据比较小,因此用树的特点即一个点入度为0其他的全为1且边的条数等于顶点数减一即可下面是我的AC代码: #include<iostream> #include <cstring> using namespace std; int main() { int n = 0, x, y, fff, i, j,sss[15], t = 0, k = 0, s ,ss, f, ff = 0, a[10005], b[10005], r[10005]; while

2016HUAS暑假集训训练题 F - 简单计算器

Description 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. Input 测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔.没有非法表达式.当一行中只有0时输入结束,相应的结果不要输出. Output 对每个测试用例输出1行,即该表达式的值,精确到小数点后2位. Sample Input 1 + 2 4 + 2 * 5 - 7 / 11 0 Sample Output 3.00 13.36 分析: 本题

2016暑假集训训练2 C题 食物链

带权并查集傻逼题 用拆点方法草过. 拆点代码: # include <stdio.h> # include <string.h> int set[150005], n, k, d; int find(int x) { int s, temp; for (s=x; set[s]>=0; s=set[s]) ; while (s != x) { temp = set[x]; set[x] = s; x = temp; } return s; } void union_set(in

2016暑假集训训练2 H题 Frosh Week

求逆序数的傻逼题. 用归并排序或者树状数组或者线段树 归并排序代码: # include<stdio.h> # define MAXN 500100 int a[MAXN],b[MAXN]; long long ans; void merge(int l, int r) { int k, p, q; if(l == r) return; int mid= (l+r)>>1; merge(l,mid); merge(mid+1,r); p = l; q = mid+1; k = l;

2016暑假集训训练2 I题 Doing Homework again

傻逼贪心题. 先按照扣分从大到小排序,分数相同则按照截止日期从小到大排序.. 然后按顺序,从截止日期开始往前找没有占用掉的时间. 如果找不到了,则加到罚分里面. # include <stdio.h> # include <stdlib.h> # include <string.h> int h[1005][2], n, visit[1005]; int comp(const void * a, const void * b) { return *(((int*)b)+

2016HUAS暑假集训训练题 E - Rails

There is a famous railway station in PopPush City. Country there is incredibly hilly. The station was built in last century. Unfortunately, funds were extremely limited that time. It was possible to establish only a surface track. Moreover, it turned

2016暑假集训训练2 N题 Play on Words

水题不解释.... # include <stdio.h> # include <string.h> int dee[26], set[26], node[26], n; char s[1002]; int find(int x) { int s, temp; for (s=x; set[s]>=0; s=set[s]) ; while (s!=x) { temp = set[x]; set[x] = s; x = temp; } return s; } void union

2016HUAS暑假集训训练2 D - 敌兵布阵

Description Lily 特别喜欢养花,但是由于她的花特别多,所以照料这些花就变得不太容易.她把她的花依次排成一行,每盆花都有一个美观值.如果Lily把某盆花照料的好的话,这盆花的美观值就会上升,如果照料的不好的话,这盆花的美观值就会下降.有时,Lily想知道某段连续的花的美观值之和是多少,但是,Lily的算术不是很好,你能快速地告诉她结果吗? Input 第一行一个整数T,表示有T组测试数据. 每组测试数据的第一行为一个正整数N(N<=50000),表示Lily有N盆花.接下来有N个正