Rabbit Party

G. Rabbit Party

Time Limit: 5000ms
Memory Limit: 65536KB
64-bit integer IO format:  %lld      Java class name:  Main

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 1 ton.

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

分析复杂度之后,暴搜满足条件的团即可。注意搜索的技巧与顺序,避免重复搜索。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;
#define INF 0x3f3f3f3f
#define N 105
#define M 105

int G[N][N];
int vis[N];
int n, m;
int ans;
int S[N];


void dfs(int u, int cnt)
{
    if(cnt >= 2)
    {
        int sum = 0;
        for(int i = 1; i <= cnt; i++)
        {
            int tmp = INF;
            for(int j = 1; j <= cnt; j++)
                if(i != j)
                {
                    tmp = min(tmp, G[S[i]][S[j]]);
                }
            sum += tmp;
        }
        ans = max(ans, sum);
    }

    for(int i = u + 1; i <= n; i++)
    {
        int ok = 1;
        for(int j = 1; j <= cnt; j++)
            if(!G[i][S[j]])
            {
                ok = 0;
                break;
            }
        if(ok)
        {
            S[cnt + 1] = i;
            dfs(i, cnt + 1);
        }
    }
}

int main()
{
    while(~scanf("%d%d", &n, &m))
    {
        memset(vis, 0, sizeof vis);
        memset(G, 0, sizeof G);
        int u, v, f;
        for(int i = 0; i < m; i++)
        {
            scanf("%d%d%d", &u, &v, &f);
            G[u][v] = G[v][u] = f;
        }
        ans = 0;
        dfs(0, 0);
        printf("%d\n", ans);
    }
    return 0;
}

/*


3 3
1 2 3
2 3 1
3 1 2

2 1
1 2 5

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

1 0


*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值