题解 CF437C

基本思路---贪心

既然要求最小代价,当用一定顺序删除时代价一定最小,不难发现,每次都删去x,y中最小的,最后的总代价业一定最小! 因此就可以写出下面的简单的代码

代码

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>//头文件 
 4 using namespace std; 
 5 int n,m,val[1025]/*权值*/,ans,x,y; 
 6 int main()
 7 {
 8     cin>>n>>m;
 9     for(register int i=1;i<=n;i++)
10         cin>>val[i]; //输入 
11     while(m--)
12     {
13         cin>>x>>y; 
14         ans+=min(val[x],val[y]); //每步求最小 
15     }
16     cout<<ans<<endl;
17     return 0; 
18 }

是不是很简单呢?

posted @ 2019-08-15 21:57  EdenHazard  阅读(247)  评论(0编辑  收藏  举报