您的位置:首页 > 其它

UVaLive 6623 Battle for Silver (最大值,暴力)

2016-08-21 14:12 387 查看
题意:给定一个图,让你找一个最大的子图,在这个子图中任何两点都有边相连,并且边不交叉,求这样子图中权值最大的是多少。

析:首先要知道的是,要想不交叉,那么最大的子图就是四个点,否则一定交叉,然后就暴力就好,数据水,不会TLE的,才100多ms

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
using namespace std;

typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 450 + 5;
const int mod = 1e9 + 7;
const char *mark = "+-*";
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
vector<int> G[maxn];

int a[maxn];
int g[maxn][maxn];
int ans;
int pre[10];

void dfs(int u, int d, int val){
ans = Max(ans, val);
if(d == 4)  return ;

for(int i = 0; i < G[u].size(); ++i){
int v = G[u][i];
bool ok = true;
for(int j = 0; j < d; ++j){
if(!g[v][pre[j]]){ ok = false;  break; }
}
for(int j = 0; j < d; ++j)
if(pre[j] == v){ ok = false;  break; }
if(!ok)  continue;
pre[d] = v;
dfs(v, d+1, val+a[v]);
}
}

int main(){
while(scanf("%d %d", &n, &m) == 2){
for(int i = 1; i <= n; ++i){
scanf("%d", &a[i]);
G[i].clear();
}
memset(g, 0, sizeof(g));
for(int i = 0; i < m; ++i){
int u, v;
scanf("%d %d", &u, &v);
g[u][v] = g[v][u] = 1;
G[u].push_back(v);
G[v].push_back(u);
}
ans = 0;
for(int i = 1; i <= n; ++i){
pre[0] = i;
dfs(i, 1, a[i]);
}
printf("%d\n", ans);
}
return 0;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: