4252. [five] QYQ school entrance exam 7day2 Figure


(File IO): input:graph.in output:graph.out

Time Limits: 1000 ms  Memory Limits: 262144 KB  Detailed Limits  

Description

You give a n points, without edges to FIG. M, each point has a non-negative weights CI, now you need to select some point, such that each point is satisfied:
If the point is not selected, and it has edges that connect all points must be selected.
Q: The point of focus to meet the above conditions, the weight of all the selected point and the minimum is how much?
QYQ quickly solved the problem, but he has returned to the lower left corner ...... left no answers, but to ask you now to solve this problem!

Input

Input data from a file graph.in in.
The first line of input contains two integers n, m
of the second input line comprises n integers, where ci represents an integer of i-th
third input to the second row m + 2 rows, each row comprising two integers u, v there is an edge between the representative points u and v

Output

Graph.out the output to a file.
The first line of output contains an integer representing the minimum weight and

Sample Input

3 1
1 2 3
3 1

Sample Output

1
Sample Description:
Select only 1 point to meet the meaning of the questions

Data Constraint

For 20% of the data: n <= 10
to 40% of the data: n <= 20
to 100% of the data: 1 <= n <= 50 , 1 <= m <= 500, 0 <= c <= 1000
in FIG. there may be multiple edges, loopback.
Points numbered 1-n.

Source / Author: Beijing Normal University Experimental graph

 

answer:

Violence pruning:

First Enumeration number status point 1, and then enumerate the number of state points 2,. . .

The status of each point: {election, not selected}

The first random.

The second condition is the point connected to it are elected.

While doing record the current and sum, if the sum of the answers gifted than before, directly back.

#include<bits/stdc++.h>
#define N 100010
#define inf 2147483647
#define rint register int
#define point(a) multiset<a>::iterator 
#define mem(a,b) memset(a,b,sizeof (a))
#define mcy(a,b) memcpy((a) , (b) ,sizeof (a));
#define open(x) freopen(x".in","r",stdin);freopen(x".out","w",stdout);
using namespace std;

struct edge
{int v,fr;}e[N*2];

int n,m,i,j,ans,t,tot,p,u,v;
int c[N],bz,a[N],tail[N],h[N];

inline void add(int u,int v)
{
	e[++tot].v=v;
	e[tot].fr=tail[u];
	tail[u]=tot;
}

void dg(int m,int sum)
{
	if(sum > ans)return ;
	if(m>n) ans=sum;
	else 
	{
		rint p=tail[m];
		for(;p;p=e[p].fr)	if(e[p].v <= m && !a[e[p].v]) break;
		if(p==0)dg(m+1,sum); //当前点不选的情况,需要与其相邻 的点都选 
		a[m]=1,dg(m+1,sum+c[m]),a[m]=0;
	}
}

int main()
{
	open("graph");
	scanf("%d%d",&n,&m);
	for(i=1;i<=n;i++)scanf("%d",&c[i]),ans+=c[i];
	for(i=1;i<=m;i++) scanf("%d%d",&u,&v),add(u,v),add(v,u);
	dg(1,0);
	printf("%d",ans);
	return 0;
}

O (can live) I ran 910ms

Guess you like

Origin blog.csdn.net/Com_man_der/article/details/86705355