Network Saboteur(随机数算法)

3 篇文章 0 订阅
1 篇文章 0 订阅


Network Saboteur
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 8119 Accepted: 3675

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

Source

Northeastern Europe 2002, Far-Eastern Subregion

完全裸的dfs差一点超时:

其实这题目比较奇葩的是如果改成dsf(0)也就是多跑一层就会超时

2531Accepted320K1657MSC++1165B

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <sstream>
#include <vector>
#include <stack>
#include <map>
#include <queue>
#include <set>
#include <iostream>
#include <stdlib.h>
#include <cctype>
#define rep(i,s,e) for(int i = (s);i<(e);++i)
#define maxn 2000
#define INF 1000000000
#define eps 1e-8
using namespace std;
int n;
int maze[maxn][maxn];
bool used[maxn];
int ans;
void dfs(int step)//step代表访问到第几个数(每个数有两种可能(在两个集合中的一个))
{
    if(step == n)
    {
        int sum = 0;
        for(int i = 0;i<n;++i)
         for(int j = 0;j<n;++j)
         {
             if(used[i]&&!used[j])
              sum += maze[i][j];
         }
        if(sum > ans)
        ans = sum;
        return ;
    }

    used[step] = 1;
    dfs(step+1);
    used[step] = 0;
    dfs(step+1);
}
int main()
{
    //freopen("in.txt","r",stdin);
    while(scanf("%d",&n)==1)
    {
       ans = 0;
       memset(used,0,sizeof(used));
       for(int i = 0;i<n;++i)
        for(int j = 0;j<n;++j)
        scanf("%d",&maze[i][j]);
        dfs(1);
        cout<<ans<<endl;

    }
    return 0;
}


转载一个用随机化做的比上一个快好多:

大概可以知道随机化算法是啥意思了:

转自:http://blog.csdn.net/lyy289065406/article/details/6648571

//Memory Time
//188K   375MS 

#include<iostream>
using namespace std;

const int TimeLimit=2000;  //本题时间限制为2000ms

int main(int i,int j)
{
	int n;
	while(cin>>n)
	{
		/*Input*/

		int w[30][30]={0};
		for(i=1;i<=n;i++)
			for(j=1;j<=n;j++)
			{
				cin>>w[i][j];
				w[j][i]=w[i][j];  //双向完全图
			}

		/*Random Algorithm*/

		bool subset[30]={false};    //A集:true  B集:false
		int time=TimeLimit*100;  //使随机次数尽可能大,随机结果尽可能接近最优解
		long max_w=0;   //最大割的权值之和
		long sum=0;  //当前边割集权和

		while(time--)
		{
			int x=rand()%n+1;  //生成随机数 x,对应于总集合的某个结点x
			                   //注意由于使用的结点序号为1~n,对应了数组下标,下标为0的数组元素没有使用
			                   //那么这里必须+1,因为若rand()=n,那么再对n取模结果就为0
			                   //这时就会导致使用了不存在的 [0]结点,本应使用的 [n]结点就被丢弃了

			subset[x]=!subset[x];  //改变x所在的集合位置

			for(int i=1;i<=n;i++)   //由于是完全图,所以每个顶点i都与x相关联,因此要全部枚举
			{
				if(subset[i]!=subset[x])   //结点i 和 x分别在两个集合内
					sum+=w[i][x];   //就是说因为x所在集合的改变,使得割边的个数增加
				                    //割集的原权值 要加上 当前新加入的割边(i,x)的权值

				if(i!=x && subset[i]==subset[x])  //结点i 和 x分别在相同的集合内,但他们不是同一元素
					sum-=w[i][x];   //就是说因为x所在集合的改变,使得割边的个数减少
				                    //割集的原权值 要减去 当前失去的割边(i,x)的权值
			}

			if(max_w < sum)
				max_w = sum;
		}

		cout<<max_w<<endl;
	}
	return 0;
}


想用贪心做。。wa了

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <sstream>
#include <vector>
#include <stack>
#include <map>
#include <queue>
#include <set>
#include <iostream>
#include <stdlib.h>
#include <cctype>
#define rep(i,s,e) for(int i = (s);i<(e);++i)
#define maxn 2000
#define INF 1000000000
#define eps 1e-8
using namespace std;
int n;
int maze[maxn][maxn];
bool used[maxn];
vector<int> a;
vector<int> b;
int main()
{
    //freopen("in.txt","r",stdin);
    while(scanf("%d",&n)==1)
    {
       int x,y;
       a.clear();
       b.clear();
       memset(used,0,sizeof(used));
       int max_n = -1;
       for(int i = 0;i<n;++i)
        for(int j = 0;j<n;++j)
        {
            scanf("%d",&maze[i][j]);
            if(max_n < maze[i][j])
            {
                max_n = maze[i][j];
                x = i;
                y = j;
            }
        }
     a.push_back(x);
     b.push_back(y);
     used[x] = true;
     used[y] = true;

     for(int i = 0;i<n;++i)
     {
         int suma = 0;
         int sumb = 0;
         if(!used[i])
         {
             for(int j = 0;j<a.size();++j)
             {
                 suma += maze[i][a[j]];
             }
              for(int j = 0;j<b.size();++j)
             {
                 sumb += maze[i][b[j]];
             }
             if(sumb > suma)
             {
                 a.push_back(i);
                 max_n += sumb;
             }else
             {
                b.push_back(i);
                max_n += suma;
             }
             used[i] = true;
         }
     }
     cout<<max_n<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智慧校园2.0是高校信息化建设的新阶段,它面对着外部环境变化和内生动力的双重影响。国家战略要求和信息技术的快速发展,如云计算、大数据、物联网等,为智慧校园建设提供了机遇,同时也带来了挑战。智慧校园2.0强调以服务至上的办学理念,推动了教育模式的创新,并对传统人才培养模式产生了重大影响。 智慧校园建设的解决之道是构建一个开放、共享的信息化生态系统,利用互联网思维,打造柔性灵活的基础设施和强大的基础服务能力。这种生态系统支持快速迭代的开发和持续运营交付能力,同时注重用户体验,推动服务创新和管理变革。智慧校园的核心思想是“大平台+微应用+开放生态”,通过解耦、重构和统一运维监控,实现服务复用和深度融合,促进业务的快速迭代和自我演化。 智慧校园的总体框架包括多端协同,即“端”,它强调以人为中心,全面感知和捕获行为数据。这涉及到智能感知设备、超级APP、校园融合门户等,实现一“码”或“脸”通行,提供线上线下服务端的无缝连接。此外,中台战略是智慧校园建设的关键,包括业务中台和数据中台,它们支持教育资源域、教学服务域等多个领域,实现业务的深度融合和数据的全面治理。 在技术层面,智慧校园的建设需要分期进行,逐步解耦应用,优先发展轻量级应用,并逐步覆盖更多业务场景。技术升级路径包括业务数据化、数据业务化、校园设施智联化等,利用IoT/5G等技术实现设备的泛在互联,并通过人工智能与物联网技术的结合,建设智联网。这将有助于实现线上线下一网通办,提升校园安全和学习生活体验,同时支持人才培养改革和后勤管理的精细化。 智慧校园的建设不仅仅是技术的升级,更是对教育模式和管理方式的全面革新。通过构建开放、共享的信息化生态系统,智慧校园能够更好地适应快速变化的教育需求,提供更加个性化和高效的服务,推动教育创新和人才培养的高质量发展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值