codeforces 574B Bear and Three Musketeers
http://codeforces.com/problemset/problem/574/B 题意:给定一个无相图。选出三个点,使得这三个点之间互相有边相连,且三个点的度数之和最小。 思路:暴力出奇迹。复杂度o(n2+n*m)
/* ***********************************************
Author :111qqz
Created Time :2015年12月09日 星期三 21时33分28秒
File Name :code/cf/problem/574B.cpp
************************************************ */
1#include <cstdio>
2#include <cstring>
3#include <iostream>
4#include <algorithm>
5#include <vector>
6#include <queue>
7#include <set>
8#include <map>
9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
1using namespace std;
2const double eps = 1E-8;
3const int dx4[4]={1,0,0,-1};
4const int dy4[4]={0,-1,1,0};
5const int inf = 0x3f3f3f3f;
6const int N=4E3+7;
7int n ,m;
8vector<int>edge[N];
9int ans;
1bool conc[N][N];
2int d[N];
3int main()
4{
5 #ifndef ONLINE_JUDGE
6 freopen("code/in.txt","r",stdin);
7 #endif
1 cin>>n>>m;
2 ms(conc,false);
3 ms(d,0);
4 for ( int i = 0 ; i < m ;i++)
5 {
6 int x,y;
7 cin>>x>>y;
8 conc[x][y] = true;
9 conc[y][x] = true;
10 d[x]++;
11 d[y]++;
12 }
13 int ans = inf;
14 for ( int i = 1 ; i <= n ; i++)
15 for ( int j = 1 ; j <= n ;j++)
16 if (conc[i][j]&&d[i]+d[j]<ans)
17 {
18 for ( int k = 1 ; k <= n ; k++)
19 if (conc[i][k]&&conc[j][k])
20 ans = min(ans,d[i]+d[j]+d[k]);
21 }
22 if (ans!=inf)
23 cout<<ans-6<<endl;
24 else puts("-1");
1 #ifndef ONLINE_JUDGE
2 fclose(stdin);
3 #endif
4 return 0;
5}