Codeforces Round #247(Div. 2) B. Shower Line 暴力

 B. Shower Line
 time limit per test
1 second
 memory limit per test
256 megabytes
 input
standard input
 output
standard output

Many students live in a dormitory. A dormitory is a whole new world of funny amusements and possibilities but it does have its drawbacks.

There is only one shower and there are multiple students who wish to have a shower in the morning. That's why every morning there is a line of five people in front of the dormitory shower door. As soon as the shower opens, the first person from the line enters the shower. After a while the first person leaves the shower and the next person enters the shower. The process continues until everybody in the line has a shower.

Having a shower takes some time, so the students in the line talk as they wait. At each moment of time the students talk in pairs: the(2i - 1)-th man in the line (for the current moment) talks with the (2i)-th one.

Let's look at this process in more detail. Let's number the people from 1 to 5. Let's assume that the line initially looks as 23154 (person number 2 stands at the beginning of the line). Then, before the shower opens, 2 talks with 3, 1 talks with 5, 4 doesn't talk with anyone. Then 2 enters the shower. While 2 has a shower, 3 and 1 talk, 5 and 4 talk too. Then, 3 enters the shower. While 3 has a shower, 1 and 5 talk, 4 doesn't talk to anyone. Then 1 enters the shower and while he is there, 5 and 4 talk. Then 5 enters the shower, and then 4 enters the shower.

We know that if students i and j talk, then the i-th student's happiness increases by gij and the j-th student's happiness increases by gji. Your task is to find such initial order of students in the line that the total happiness of all students will be maximum in the end. Please note that some pair of students may have a talk several times. In the example above students 1 and 5 talk while they wait for the shower to open and while 3 has a shower.

Input

The input consists of five lines, each line contains five space-separated integers: the j-th number in the i-th line shows gij (0 ≤ gij ≤ 105). It is guaranteed that gii = 0 for all i.

Assume that the students are numbered from 1 to 5.

Output

Print a single integer — the maximum possible total happiness of the students.

Examples
input
0 0 0 0 9
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
7 0 0 0 0
output
32
input
0 43 21 18 2
3 0 21 11 65
5 2 0 1 4
54 62 12 0 99
87 64 81 33 0
output
620
Note

In the first sample, the optimal arrangement of the line is 23154. In this case, the total happiness equals:

(g23 + g32 + g15 + g51) + (g13 + g31 + g54 + g45) + (g15 + g51) + (g54 + g45) = 32
.
题意:
5个人排队洗澡,有先后顺序,在shower开之前,第1个人和第2个人交谈,第3个和第4个交谈,第5个不交谈,shower打开之后,第1个人开始洗澡,则第2个人和第3个人交谈,第4个和第5个交谈,以此类推。
交谈时相互的,双方都会增加happy值,分别为Gij和Gji(两者不一定相等),给出G矩阵,求出5个人怎么样排队能得出最大happy值,只输入最大值即可。
题解:
以为只要5个人,用next_permutation()枚举排列,求解更新最大值答案res

/****************
*PID:431b div2
*Auth:Jonariguez
*****************
next_permutation 暴力
*/
#define lson k*2,l,m
#define rson k*2+1,m+1,r
#define rep(i,s,e) for(i=(s);i<=(e);i++)
#define For(j,s,e) For(j=(s);j<(e);j++)
#define sc(x) scanf("%d",&x)
#define In(x) scanf("%I64d",&x)
#define pf(x) printf("%d",x)
#define pfn(x) printf("%d\n",(x))
#define Pf(x) printf("%I64d",(x))
#define Pfn(x) printf("%I64d\n",(x))
#define Pc printf(" ")
#define PY puts("YES")
#define PN puts("NO")
#include <stdio.h>
#include <string.h>
#include <string>
#include <math.h>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef int Ll;
Ll quick_pow(Ll a,Ll b,Ll MOD){a%=MOD;Ll res=1;while(b){if(b&1)res=(res*a)%MOD;b/=2;a=(a*a)%MOD;}return res;}

const int maxn=100000+10;
int a[10][10],p[maxn];

int main()
{
    int i,j,n,res;
    for(i=1;i<=5;i++)
        for(j=1;j<=5;j++) sc(a[i][j]);
    res=0;
    for(i=1;i<=5;i++) p[i]=i;
    do{
        int sum=0;
        for(i=1;i<=5;i++){
            for(j=i;j<=5;j+=2){
                int x=p[j],y=p[j+1];
                sum+=a[x][y]+a[y][x];
            }
        }
        res=max(res,sum);
    }while(next_permutation(p+1,p+6));
    printf("%d\n",res);
    return 0;
}


猜你喜欢

转载自blog.csdn.net/u013068502/article/details/51065794