UESTC 889 Battle for Silver (dfs)

题意:

给一个图,每个点有点权,每两个点最多有一条边相连,每个点至少和一个点通过边相连。

要找出这样一个团,使得团内所有的点两两都有边相连且边不交叉,并且点权最大。

算法:

由于两两连边且边不能交叉,可知最多有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 500
using 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;
}
时间: 2024-08-06 14:59:50

UESTC 889 Battle for Silver (dfs)的相关文章

CDOJ 889 Battle for Silver

Battle for Silver Time Limit: 2999/999MS (Java/Others)     Memory Limit: 65432/65432KB (Java/Others) Piet Hein was a Dutch naval officer during the Eighty Years' War between the United Provinces of The Netherlands and Spain. His most famous victory w

poj1856Sea Battle(DFS)

题目链接: huangjing 思路: 这个题目当时想到是找联通快,但是不知道怎么判断这个联通快是不是标准的好船,后来看了别人的题解才知道可以用面积去判断...这个知道了就是简单的dfs找联通快了..注意是如果出现一艘破船则不用找了,直接输出就可以了... 题目: Sea Battle Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 2809   Accepted: 996 Description During the S

POJ 1856 Sea Battle(dfs)

Description During the Summit, the armed forces will be highly active. The police will monitor Prague streets, the army will guard buildings, the Czech air space will be full of American F-16s. Moreover, the ships and battle cruisers will be sent to

poj3187Backward Digit Sums(DFS)

题目链接: huangjing 思路: 这个题目想到dfs很容易,但是纠结在这么像杨辉三角一样计算那些值,这个我看的队友的,简直厉害,用递归计算出杨辉三角顶端的值....具体实现详见代码... 题目: Language: Default Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4285   Accepted: 2474 Description FJ and his cows

POJ 3087 Shuffle&#39;m Up (DFS)

题目链接:Shuffle'm Up 题意:有a和b两个长度为n的字符序列,现定义操作: 将a.b的字符交叉合并到一个序列c,再将c最上面的n个归为a,最下面n个归为b 给出a,b和目标序列c,问最少多少次操作a.b转化为c 解析:将a.b放入哈希表,然后模拟操作过程直接dfs即可. AC代码: #include <cstdio> #include <iostream> #include <cstring> #include <map> using names

LeetCode Subsets (DFS)

题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. 1 class Solution { 2 public: 3 vector<vector<int>> subsets(vector<int>& nums) { 4 sort(nums.begin(),nums.end()); 5 DFS(0,nums,tmp); 6 return a

poj1753 Flip Game(枚举Enum+dfs)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=1753 Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the o

POJ 3411 Paid Roads(dfs)

*注:这一题很重要的是对与数据的处理和细节的把握把! http://poj.org/problem?id=3411 题目大意: 有n个城市,m条路,(0<=n,m<=10).从a到b,如果之前已经经过c点,那么付费p,否者付费r.求最小的费用,从1-->n! 注意: There may be more than one road connecting one city with another. so:你不能用map[a][b]存a->b的距离.只能有road [ i ]了. 还有

POJ 1699 Best Sequence(DFS)

題目鏈接 題意 : 將幾個片段如圖所示方法縮成一個序列,求出最短這個序列. 思路 : 其實我也不知道怎麼做.....看網上都用了DP.....但是我不會.....這個DP不錯,還有用KMP+状压DP做的 1 //1699 2 #include <iostream> 3 #include <stdio.h> 4 #include <string.h> 5 #include <string> 6 7 using namespace std; 8 9 string