The Child and Toy

Codeforces Round #250 (Div. 2) C:http://codeforces.com/problemset/problem/437/C

题意:给以一个无向图,每个点都有一点的权值,然后如果要删除一个点的话,会有一定的费用,这个费用是与这个点的相邻的,并且是没有删除的点权值之和。

题解:很简单的,肯定是贪心,因为为了避免权值最大的点对其他点造成影响,所以首先删除的应该是权值最大值。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 const int N=1002;
 7 int n,m,ans;
 8 bool mp[N][N];
 9 int a[N],t1,t2;
10 struct Node{
11   int val;
12   int id;
13   bool operator<(const Node a)const{
14      return val>a.val;
15   }
16 }num[N];
17 int main(){
18   scanf("%d%d",&n,&m);
19   memset(mp,0,sizeof(mp));
20   ans=0;
21   for(int i=1;i<=n;i++){
22     scanf("%d",&num[i].val);
23     num[i].id=i;
24     a[i]=num[i].val;
25   }
26    for(int i=1;i<=m;i++){
27     scanf("%d%d",&t1,&t2);
28     mp[t1][t2]=mp[t2][t1]=1;
29    }
30    sort(num+1,num+n+1);
31    for(int i=1;i<=n;i++){
32       int temp=num[i].id;
33       for(int i=1;i<=n;i++){
34          if(mp[temp][i]){
35             ans+=a[i];
36             mp[temp][i]=mp[i][temp]=0;
37          }
38       }
39    }
40    printf("%d\n",ans);
41 
42 }
View Code

 

posted on 2014-08-01 16:23  天依蓝  阅读(230)  评论(0编辑  收藏  举报

导航