Aizu - 2306 Rabbit Party (DFS图论)

G. Rabbit Party

5000ms
5000ms
65536KB
64-bit integer IO format:  %lld      Java class name:  Main
Font Size:   

Rabbit Party

A rabbit Taro decided to hold a party and invite some friends as guests. He has n rabbit friends, and m pairs of rabbits are also friends with each other. Friendliness of each pair is expressed with a positive integer. If two rabbits are not friends, their friendliness is assumed to be 0.

When a rabbit is invited to the party, his satisfaction score is defined as the minimal friendliness with any other guests. The satisfaction of the party itself is defined as the sum of satisfaction score for all the guests.

To maximize satisfaction scores for the party, who should Taro invite? Write a program to calculate the maximal possible satisfaction score for the party.

Input

The first line of the input contains two integers, n and m (1 ¥leq n ¥leq 1000 ¥leq m ¥leq 100). The rabbits are numbered from 1to n.

Each of the following m lines has three integers, uv and fu and v (1 ¥leq u, v ¥leq nu ¥neq v1 ¥leq f ¥leq 1,000,000) stands for the rabbits' number, and f stands for their friendliness.

You may assume that the friendliness of a pair of rabbits will be given at most once.

Output

Output the maximal possible satisfaction score of the party in a line.

Sample Input 1

3 3
1 2 3
2 3 1
3 1 2

Output for the Sample Input 1

6

Sample Input 2

2 1
1 2 5

Output for the Sample Input 2

10

Sample Input 3

1 0

Output for the Sample Input 3

0

Sample Input 4

4 5
1 2 4
1 3 3
2 3 7
2 4 5
3 4 6

Output for the Sample Input 4

16
分析题目,由于给你的完全图(即一个顶点与其他的顶点都有边)
接着从题目可以分析的n * (n - 1) / 2 = m <= 100
(提示,如果一个点与其他点之间的友好值为0,那么可以去掉这个点,或者那个与它的边为0的点,大家可以思考一下,所以为零的可以直接去掉)
得知n <= 15所以,实际上只有15个数,那么我们可以DFS,得到这些数都是不会超时的

#include <map>
#include <set>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include <algorithm>
using namespace std;


#define pb push_back
#define mp make_pair
#define fillchar(a, x) memset(a, x, sizeof(a))
#define copy(a, b) memcpy(a, b, sizeof(a))
#define S_queue<P> priority_queue<P, vector<P>,greater<P> >


typedef long long LL;
typedef pair<int, int > PII;
typedef unsigned long long uLL;
template<typename T>
void print(T* p, T* q, string Gap = " "){int d = p < q ? 1 : -1;while(p != q){cout << *p;p += d;if(p != q) cout << Gap; }cout << endl;}
template<typename T>
void print(const T &a, string bes = "") {int len = bes.length();if(len >= 2)cout << bes[0] << a << bes[1] << endl;else cout << a << endl;}

const int INF = 0x3f3f3f3f;
const int MAXM = 1e2 + 5;
const int MAXN = 1e2 + 5;
int Fig[MAXN][MAXN], Max;
int X[25], n, m;

void Deal(int s){
    int f , ans = 0;
    for(int i = 1;i <= s;i ++){
        f = INF;
        for(int j = 1;j <= s;j ++){
            if(i == j) continue;
            f = min(f, Fig[X[i]][X[j]]);
        }
    if(f != INF)
        ans += f;
    }
    Max = max(ans, Max);
}
void DFS(int u){
    Deal(u);
    int st = X[u] + 1;
    if(u == 0) st = 1;
    for(int i = st;i <= n;i ++){
        bool flag = true;
        for(int j = 1;j < u;j ++){
            if(Fig[i][X[j]] == 0) {
                flag = false;
                break;
            }
        }
        X[u + 1] = i;
        if(flag) DFS(u + 1);
    }
}

int main(){
    int u, v, d;
    while(cin >> n >> m){
        Max = 0;
        memset(Fig, 0, sizeof(Fig));
        for(int i = 1;i <= m;i ++){
            cin >> u >> v >> d;
            Fig[u][v] = Fig[v][u] = d;
            Max = max(Max, d * 2);
        }
        DFS(0);
        print(Max);
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值