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


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

    析:首先要知道的是,要想不交叉,那么最大的子图就是四个点,否则一定交叉,然后就暴力就好,数据水,不会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
    ", ans);
            }
            return 0;
        }
    

      

  • 相关阅读:
    基于Tire树和最大概率法的中文分词功能的Java实现
    通过 Spring RestTemplate 调用带请求体的 Delete 方法(Delete With Request Body)
    《高效能程序员的修炼》之译者序(本书将于7月初上市,人民邮电出版社)
    JVM类加载过程学习总结
    Web安全之Cookie劫持
    2016第40周五
    年轻By塞缪尔·厄尔曼
    2016第40周三
    2016第40周二
    果断歌
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/5792664.html
Copyright © 2020-2023  润新知