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