UESTC 889 Battle for Silver (dfs)
来源:互联网 发布:沪江日语网络课程 编辑:程序博客网 时间:2024/05/29 14:10
题意:
给一个图,每个点有点权,每两个点最多有一条边相连,每个点至少和一个点通过边相连。
要找出这样一个团,使得团内所有的点两两都有边相连且边不交叉,并且点权最大。
算法:
由于两两连边且边不能交叉,可知最多有4个点。所以暴搜~
dfs出4个位置放什么元素,一边判断放的点与前面的点是否是两两连边,一边更新ans。
开始一直当做3个点和4个点在写,忘了考虑1个点和2个点。。。WA了10次。。
自己写个样例自己推结果也是没考虑1个点和2个点的情况。。
/*
6 7
1500
1000
100
2000
500
300
1 2
1 3
1 4
3 5
4 5
4 6
5 6
ans = 3500
*/
#include<cstdio>#include<iostream>#include<cstring>#include<vector>#define maxn 500using namespace std;int a[maxn],mp[maxn][maxn],v,ans;int op[5];bool vis[maxn];int check(int pos,int x){ for(int i=0;i<pos;i++) { if(!mp[op[i]][x]) return 0; } return 1;}void dfs(int pos,int x,int sum){ ans = max(ans,sum); if(pos==4) return; for(int i=x+1;i<=v;i++) { if(vis[i] || !check(pos,i)) continue; op[pos] = i; vis[i] = true; dfs(pos+1,i,sum+a[i]); vis[i] = false; }}int main(){ int e,ta,tb; while(scanf("%d%d",&v,&e)!=EOF) { memset(mp,0,sizeof(mp)); for(int i=1;i<=v;i++) scanf("%d",&a[i]); for(int i=1;i<=e;i++) { scanf("%d%d",&ta,&tb); mp[ta][tb] = mp[tb][ta] = 1; } ans = 0; dfs(0,0,0); printf("%d\n",ans); } return 0;}
0 0
- UESTC 889 Battle for Silver (dfs)
- UESTC889&&uvalive6623 Battle for Silver(dfs)
- nwerc2013 B - Battle for Silver
- UVAlive 6623 Battle for Silver(暴力+思路)
- poj1856Sea Battle(DFS)
- CDOJ1590-dfs+树链剖分(2017 UESTC Training for Data Structures O)
- POJ 1856 Sea Battle(dfs)
- UESTC--1252--24点游戏(dfs)
- UESTC - 1039 Fabricate equation (DFS&模拟)
- (CCPC F题)UESTC 1220 The Battle of Guandu (最短路)
- pat甲1013. Battle Over Cities(dfs)
- 1013. Battle Over Cities (25)(并查集/dfs)
- UESTC Treasure----dfs
- UESTC OJ1221(DFS)
- UESTC--1262--Memory(dfs)
- UESTC Training for Search Algorithm(总结)
- POJ 1856 Sea Battle(DFS)
- POJ 1856 Sea Battle(DFS)
- STL vector方法总结(一)Member functions(34)
- XML 头声明和standalone="yes"属性介绍
- Android自定义组合控件--EditText和Button组合成带有清空EditText内容功能的复合控件
- apache common book 笔记
- 安装pymysql,执行setup.py提示no commands supplied
- UESTC 889 Battle for Silver (dfs)
- Codeforces Round #263 (Div. 1) B. Appleman and Tree
- php 添加环境变量
- Android 命名规范 (提高代码可以读性)
- LeetCode OJ - Merge Intervals
- 24yw的博客开张~写点感言
- 硬件调试
- linux gdb 调试
- 运行命令热键