zoukankan      html  css  js  c++  java
  • Codeforces Round #636 (Div. 3) E —Weights Distributing

    //无论a到b 和 b到c 有没有重合
    //都可以写为
    //a->x->b->x->c
    #include<queue>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    char buf[1<<15],*fs,*ft;
    inline char getc()
    {
    	return (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<15,stdin),fs==ft))?0:*fs++;
    }
    inline int read()
    {
    	int x=0,f=1;
    	char ch=getc();
    	while(ch<'0'||ch>'9')
    	{
    		if(ch=='-')f=-1;
    		ch=getc();
    	}
    	while(ch>='0'&&ch<='9')
    	{
    		x=x*10+ch-'0';
    		ch=getc();
    	}
    	return x*f;
    }
    const int N=4e5+7;
    const ll mod=998244353;
    ll pre[N];
    int num[N];
    int dis1[N],dis2[N],dis3[N];
    int vis[N];
    int e[N],ne[N],idx,h[N];
    int T;
    int n,m,a,b,c;
    void add(int a,int b)
    {
    	e[idx]=b;
    	ne[idx]=h[a];
    	h[a]=idx++;
    }
    void bfs(int x,int dis[])
    {
    	for(int i=1; i<=n; i++)
    		vis[i]=0;
    	dis[x]=0;
    	vis[x]=1;
    	queue<int>q;
    	q.push(x);
    	while(q.size())
    	{
    		int now=q.front();
    		q.pop();
    		for(int j=h[now]; j!=-1; j=ne[j])
    		{
    			int i=e[j];
    			if(vis[i]==0)
    			{
    				vis[i]=1;
    				dis[i]=dis[now]+1;
    				q.push(i);
    			}
    		}
    	}
    }
    int main()
    {
    	T=read();
    	while(T--)
    	{
    		idx=0;
    		n=read(),m=read(),a=read(),b=read(),c=read();
    		for(int i=0; i<=n; i++)
    			h[i]=-1;
    		for(int i=1; i<=m; i++)
    			num[i]=read();
    		for(int i=1; i<=m; i++)
    		{
    			int a=read(),b=read();
    			add(a,b);
    			add(b,a);
    		}
    		sort(num+1,num+1+m);
    		//边权的前缀和
    		for(int i=1; i<=m; i++)
    			pre[i]=pre[i-1]+num[i];
    		// cout<<endl;
    		//以a为起点,到各个点的距离,先默认边权为1
    		bfs(a,dis1);
    		bfs(b,dis2);
    		bfs(c,dis3);
    		ll ans=1e18;
    		for(int x=1; x<=n; x++)
    		{
    			if(dis1[x]+dis2[x]+dis3[x]>m)
    				continue;
    			ans=min(ans,pre[dis1[x]+dis2[x]+dis3[x]]+pre[dis2[x]]);
    		}
    		cout<<ans<<endl;
    	}
    	return 0;
    }    
    
  • 相关阅读:
    charles 安装、破解、简单介绍
    8、postman中 转码生成python-requests接口请求代码,并定义一个获取及请求的方法
    json 序列化和反序列化(针对python数据类型)
    leetcode 35.搜索插入位置
    leetcode 27.移除元素
    js 中的数组方法
    js判断小数点后几位小数
    leetcode 15.三数之和
    leetcode 1.两数之和
    leetcode 680.验证回文字符串
  • 原文地址:https://www.cnblogs.com/QingyuYYYYY/p/12777372.html
Copyright © 2011-2022 走看看