校内模拟测试010T1 删点游戏dt

题意简述

n个点m条边的无向图,要把所有点一个一个地删去。每次删去一个点的花费为这个点相邻的还未被删除的点的点权。无重边无自环,求最小代价。

数据范围

对于\(30\%\)的数据\(n \le 10\)

对于\(60\%\)的数据\(n,m \le 1000\)

对于\(100\%\)的数据\(1\le n,m,a_i\le100000\)

分析

如果从点的角度来考虑,是无法得出贪心的结果的。考虑将点权转化为边权。

可以发现,最后每一条边都是要被删去的。删掉一个点的代价是它的邻点,而对于一条边,我们肯定要贪心地让其删去的代价最小。故删去一条边的代价为连接的两个点的最小值。

Code

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<queue>
#include<vector>
#define IL inline
#define re register
#define LL long long
#define ULL unsigned long long
#define re register
#define debug printf("Now is %d\n",__LINE__);
using namespace std;

template<class T>inline void read(T&x)
{
    char ch=getchar();
    while(!isdigit(ch))ch=getchar();
    x=ch-'0';ch=getchar();
    while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
}
inline int read()
{
	int x=0;
    char ch=getchar();
    while(!isdigit(ch))ch=getchar();
    x=ch-'0';ch=getchar();
    while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
    return x;
}
int G[55];
template<class T>inline void write(T x)
{
    int g=0;
    if(x<0) x=-x,putchar('-');
    do{G[++g]=x%10;x/=10;}while(x);
    for(re int i=g;i>=1;--i)putchar('0'+G[i]);putchar('\n');
}
LL n,m,ans;
LL a[100010]; 
int main()
{
	n=read();
	m=read();
	for(re LL i=1;i<=n;i++) a[i]=read();
	for(re LL i=1;i<=m;i++)
	{
		ans+=min(a[read()],a[read()]);
	}
	cout<<ans;
	return 0;
}
posted @ 2020-11-05 13:39  Vanilla_chan  阅读(187)  评论(1)    收藏  举报