题目
分析
- 暴力
- 但是要注意优化 sum>=ans
- 然后。。。
代码
1 #pragma GCC optimize(2)
2 #include<fstream>
3 #include<cstring>
4 #include<vector>
5 #include<cstdio>
6 #include<algorithm>
7 #define ll long long
8 using namespace std;
9 ll n,m;
10 vector <int> f[10001];
11 ll a[10001],b[10001];
12 ll flag[10001];
13 ll anss=0x3f3f3f3f,cnt,ans;
14 void dfs(ll p,ll sum)
15 {
16 if (sum>=anss) return;
17 if (p>n) {anss=sum;return;}
18 flag[p]++; dfs(p+1,sum+a[p]); flag[p]--;
19 if (!flag[p])
20 {
21 for (int i=0;i<f[p].size();i++)
22 flag[f[p][i]]++;
23 dfs(p+1,sum);
24 for (int i=0;i<f[p].size();i++)
25 flag[f[p][i]]--;
26 }
27 }
28 inline ll read()
29 {
30 ll p=0,f=1;char c=getchar();
31 while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
32 while(c>='0'&&c<='9'){p=p*10+c-'0';c=getchar();}
33 return f*p;
34 }
35 int main ()
36 {
37 freopen("graph.in","r",stdin);
38 freopen("graph.out","w",stdout);
39 n=read();
40 m=read();
41 for (int i=1;i<=n;i++)
42 a[i]=read();
43 for (int i=1,x,y;i<=m;i++)
44 {
45 x=read();
46 y=read();
47 if (x==y&&!flag[x])
48 flag[x]=1;
49 f[x].push_back(y);
50 f[y].push_back(x);
51 }
52 dfs(1,0);
53 printf("%lld",anss);
54 }