Network Saboteur
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 10257 | Accepted: 4914 |
题意:有n个点,告诉你任意两点之间距离(都行),把这些点分成两份;求两份间任意两点间的距离和得最大值;
思路:可分为A ,B两份,枚举每一个分别属于A或B;(较为普世);
以下程序运行结果均在poj g++下测是得到;
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).
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 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
求两部分之间任意两点间的距离, 可理解为如果两部分店内不距离和最小即可;这样有利于优化:
可是思维无止境;可以假设所有点一开始都属于某一方;
然后往另一方移动;
但是还是可以的:
进度太大;再来一发;
是否还可以? 一切皆有可能
能否用 第一种方案的逆向思维去优化;有兴趣自己试试吧 ^-^;
<pre name="code" class="cpp">
Memory: 660K Time: 375MS
Language: G++ Result: Accepted
#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<set>
#include<map>
#include<queue>
#include<cctype>
#include<cstdlib>
#include<vector>
#define LL long long
#define uLL unsigned LL
using namespace std;
int mp[25][25];
bool flag[25],A=true,B=false;
int ans;
int n;
void DFS(int pos,int now)
{
if(pos>n) //把所有的点分配完毕;
{
if(now>ans)
ans=now;
return;
}
int sum=0;
flag[pos]=A; //假设该点属于A;
for(int i=1;i<pos;i++)
{
if(flag[i]==B)
{
sum+=mp[pos][i];
}
}
DFS(pos+1,now+sum);
sum=0;flag[pos]=B; //假设该点属于B;
for(int j=1;j<pos;j++)
{
if(flag[j]==A)
{
sum+=mp[pos][j];
}
}
DFS(pos+1,now+sum);
}
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]);
}
}
ans=0;
DFS(1,0);
printf("%d\n",ans);
}
return 0;
}
求两部分之间任意两点间的距离, 可理解为如果两部分店内不距离和最小即可;这样有利于优化:
Source Code
Problem: 2531 User: 14110103069
Memory: 660K Time: 282MS
Language: G++ Result: Accepted
#include <iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cctype>
#include<algorithm>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<cstdlib>
#define LL long long
#define uLL unsigned uLL
#define INF 0x3f3f3f3f
using namespace std;
int mp[25][25];
bool flag[25],A=true,B=false;
int ans,sum;
int n;
void DFS(int pos,int now)
{
if(now>ans)
return;
if(pos>n)
{
ans=now;
return;
}
int s=0;
flag[pos]=A;
for(int i=1;i<pos;i++)
{
if(flag[i]==A)
s+=mp[pos][i];
}
DFS(pos+1,now+s);
s=0;flag[pos]=B;
for(int j=1;j<pos;j++)
{
if(flag[j]==B)
s+=mp[pos][j];
}
DFS(pos+1,now+s);
}
int main()
{
while(~scanf("%d",&n))
{
sum=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
scanf("%d",&mp[i][j]);
sum+=mp[i][j];
}
}
ans=INF;
DFS(1,0);
printf("%d\n",sum/2-ans);
}
return 0;
}
可是思维无止境;可以假设所有点一开始都属于某一方;
然后往另一方移动;
Source Code
Problem: 2531 User: 14110103069
Memory: 660K Time: 204MS
Language: G++ Result: Accepted
Source Code
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<stack>
#include<set>
#include<cctype>
#include<list>
#define LL long long
#define uLL unsigned LL
using namespace std;
int mp[25][25];
bool flag[25];
int ans;
int n;
void DFS(int cnt,int now)
{
int tmp=now;
flag[cnt]=1;
for(int i=1;i<=n;i++)
{
if(!flag[i])
tmp+=mp[cnt][i];
else
tmp-=mp[cnt][i];
}
if(tmp>ans)
ans=tmp;
if(tmp<now)
return;
for(int i=cnt+1;i<=n;i++)
{
flag[i]=1;
DFS(i,tmp);
flag[i]=0;
}
}
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]);
}
}
memset(flag,0,sizeof(flag));
ans=0;
DFS(0,0);
printf("%d\n",ans);
}
return 0;
}
但是还是可以的:
Source Code
Problem: 2531 User: 14110103069
Memory: 660K Time: 32MS
Language: G++ Result: Accepted
Source Code
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<stack>
#include<set>
#include<cctype>
#include<list>
#define LL long long
#define uLL unsigned LL
using namespace std;
int mp[25][25];
bool flag[25];
int ans;
int n;
void DFS(int cnt,int now)
{
int tmp=now;
flag[cnt]=1;
for(int i=1;i<=n;i++)
{
if(!flag[i])
tmp+=mp[cnt][i];
else
tmp-=mp[cnt][i];
}
if(tmp>ans)
ans=tmp;
if(tmp<=now)
return;
for(int i=cnt+1;i<=n;i++)
{
flag[i]=1;
DFS(i,tmp);
flag[i]=0;
}
}
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]);
}
}
memset(flag,0,sizeof(flag));
ans=0;
DFS(1,0);
printf("%d\n",ans);
}
return 0;
}
进度太大;再来一发;
Source Code
Problem: 2531 User: 14110103069
Memory: 660K Time: 94MS
Language: G++ Result: Accepted
Source Code
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<stack>
#include<set>
#include<cctype>
#include<list>
#define LL long long
#define uLL unsigned LL
using namespace std;
int mp[25][25];
bool flag[25];
int ans;
int n;
void DFS(int cnt,int now)
{
int tmp=now;
flag[cnt]=1;
for(int i=1;i<=n;i++)
{
if(!flag[i])
tmp+=mp[cnt][i];
else
tmp-=mp[cnt][i];
}
if(tmp>ans)
ans=tmp;
if(tmp<now)
return;
for(int i=cnt+1;i<=n;i++)
{
flag[i]=1;
DFS(i,tmp);
flag[i]=0;
}
}
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]);
}
}
memset(flag,0,sizeof(flag));
ans=0;
DFS(1,0);
printf("%d\n",ans);
}
return 0;
}
是否还可以? 一切皆有可能
能否用 第一种方案的逆向思维去优化;有兴趣自己试试吧 ^-^;