H - The Child and Toy-CodeForces 437C-水题

这题看懂了直接 输出一条边中比较小的那个点的权值即可。。。当时没想到,一股脑就当成图去贪心了,所以复杂多了


 

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
// 邻接表vector实现   
struct node
{
	int val;
	int num;
} v[1000+5];
int v2[1000+5];
int cmp(node a,node b)
{
	return a.val<b.val;
}

vector <int> map[1000];
vector<int>::iterator it;
vector<int>::iterator del;
int main() 
{
	
  
		int n,m,x,y;
		scanf("%d%d",&n,&m);
		int i;
		for(i=1;i<=n;i++) 
		{
			scanf("%d",&v[i].val);
			v2[i]=v[i].val;
			v[i].num=i;
		}
		sort(v+1,v+1+n,cmp);
		for (i=1;i<=m;i++)
		{
			scanf("%d %d",&x,&y);
			map[x].push_back(y);
			map[y].push_back(x);
		}
		int sum=0;
 
		for (i=1;i<=n;i++)
		{
				
			if ( map[ v[i].num ].size()!=0)
			{
				sum+=map[ v[i].num ].size()*v2[v[i].num];
				it=map[ v[i].num ].begin();  //能亮最小的头
				for (;it!=map[ v[i].num ].end();it++)
				{	
					int t1= *it;
				del = find( map[t1].begin(), map[t1].end(), v[i].num  ); //查找
							map[t1].erase(del);
				}
				map[ v[i].num ].clear();
			}
			else
				continue; 
		  
		}
		
		printf("%d\n",sum);
	
	
	
	
	
	
	return 0;
	
}


你可能感兴趣的:(H - The Child and Toy-CodeForces 437C-水题)