寒假2019培訓:最小代價*圖論*@最小生成樹@

題目描述

給出一幅由n個點m條邊構成的無向帶權圖。 其中有些點是黑點,其他點是白點。

現在每個白點都要與他距離最近的黑點通過最短路連接(如果有很多個黑點,可以選取其中任意一個),我們想要使得花費的代價最小。

請問這個最小代價是多少?

注意:最後選出的邊保證每個白點到離它最近的黑點的距離仍然等於原圖中的最短距離。

輸入格式

第一行兩個整數n,m;

第二行n 個整數,0表示白點,1 表示黑點;

接下來m 行,每行三個整數x,y,z,表示一條連接x和y 點,權值爲z 的邊。

輸出格式

如果無解,輸出impossible;

否則,輸出最小代價。

樣例數據

input

5 7
0 1 0 1 0
1 2 11
1 3 1
1 5 17
2 3 1
3 5 18
4 5 3
2 4 5
output

5

【樣例解釋】

選 2、4、6三條邊
數據規模與約定
對30%的輸入數據: 1≤n≤10, 1≤m≤20;
對100%的輸入數據:1≤n≤100000,1≤m≤200000,1≤z≤1000000000

時間限制:1s
空間限制:256MB

添加超級源點S,S 與每個黑點間連上權值爲0的邊,先處理從S 出發到每個點的最短距離,取出一張最短路徑圖,然後再來一次最小生成樹算法

#include<bits/stdc++.h>
using namespace std;
FILE *fin,*fout;
const int N=100000+200,M=500000+20;
int n,m,color[N],Last[M],t,T,vis[N],f[N],total;
long long dis[N],ans;
priority_queue < pair<long long,int>,vector< pair<long long,int> >,greater< pair<long long,int> > > Heap;
struct EDGE{int reach,next,start,flag;long long val;}e[M];
inline void insert(int x,int y,long long v)
{
	e[++t].reach=y;e[t].val=v;e[t].next=Last[x];Last[x]=t;e[t].start=x;
}
inline int find(int x){return f[x]==x?x:f[x]=find(f[x]);}
inline bool cmp(EDGE p1,EDGE p2){return p1.val<p2.val;}
inline void input(void)
{
	fscanf(fin,"%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		fscanf(fin,"%d",&color[i]);
	for(int i=1;i<=m;i++)
	{
		int x,y;long long v;
		fscanf(fin,"%d%d%lld",&x,&y,&v);
		insert(x,y,v);
		insert(y,x,v);	
	}
} 
inline void super_node(void)
{
	for(int i=1;i<=n;i++)
	{
		if(color[i])
			insert(i,0,0LL),
			insert(0,i,0LL);
	}
}
inline void dijkstra(void)
{
	memset(dis,127,sizeof dis);
	memset(vis,0,sizeof vis);
	Heap.push(make_pair(0LL,0));
	dis[0]=0LL;
	while(!Heap.empty())
	{
		int temp=Heap.top().second;
		Heap.pop();
		if(vis[temp])continue;
		vis[temp]=true;
		for(int i=Last[temp];i;i=e[i].next)
		{
			int reach=e[i].reach;
			if(dis[reach]>dis[temp]+e[i].val)
			{
				dis[reach]=dis[temp]+e[i].val;
				Heap.push(make_pair(dis[reach],reach));
			}
		}
	}
}
inline void build(void)
{
	for(int i=0;i<=n;i++)
	{
		for(int j=Last[i];j;j=e[j].next)
		{
			int x=i,y=e[j].reach;
			if(dis[y]==dis[x]+e[j].val)
				e[j].flag=true;
		}
	}
}
inline void kruscal(void)
{
	for(int i=0;i<=n;i++)
		f[i]=i;
	sort(e+1,e+t+1,cmp);
	int cnt=0;
	for(int i=1;i<=t;i++)
	{
		if(!e[i].flag)continue;
		int x=find(e[i].start),y=find(e[i].reach);
		if(x==y)continue;
		f[x]=y;
		cnt++;
		ans+=e[i].val;
		if(cnt==n)break;
	}
}
int main(void)
{
	fin=fopen("minimum.in","r");
	fout=fopen("minimum.out","w");
	input();
	super_node();
	dijkstra();
	build();
	kruscal();
	if(ans)fprintf(fout,"%lld\n",ans);
	else fprintf(fout,"impossible\n");
	return 0;
}

*或者看看這個*~~

#include<bits/stdc++.h>
using namespace std;
const int N=200005;
long long n,m,k,len,tot,cnt;
long long f[N*2],last[N*2],vis[N*2],fa[N*2],dis[N*2];
long long ans=0;
struct ss
{
    int to,next,v,fg,x;
}e[N*4];
void insert(int x,int y,int z)
{
    e[++len].next=last[x],
	e[len].to=y,
	e[len].v=z,
	e[len].x=x,
    last[x]=len;
}
void SPFA(int x)
{ 
    queue<int>q;
	memset(dis,0x3f3f3f3f,sizeof(dis));
    dis[x]=0,vis[x]=1,q.push(x);
    while(!q.empty())
    {
        int _x=q.front(),i=last[_x];
        for(q.pop(),vis[_x]=0;i;i=e[i].next)
        {
            int v=e[i].to;
            if(dis[v]>dis[_x]+e[i].v)
            {
                dis[v]=dis[_x]+e[i].v;
                if(!vis[v])vis[v]=1,q.push(v);
            }
        }
    }
}
void dfs()
{
	int i=0,j;
    for(;++i<=n;)
    {
        if(f[i])continue;
        for(j=last[i];j;j=e[j].next)
        {
            int v=e[j].to;
            if(v==0)continue;
            if(dis[v]+e[j].v==dis[i])e[j].fg=1;
        }
    }
}
bool myc(ss xx,ss yy)
{
   return xx.v<yy.v;
}
int find(int x)
{
    return fa[x]==x?fa[x]:fa[x]=find(fa[x]);
}
int main()
{
//	freopen("minimum.in","r",stdin);
//	freopen("minimum.out","w",stdout);
	
	int i=0,j;
    for(scanf("%lld%lld",&n,&m);++i<=n;)scanf("%lld",&f[i]);
    for(i=0;++i<=m;) 
    {
        int x,y,z;
        scanf("%lld%lld%lld",&x,&y,&z);
        insert(x,y,z),
		insert(y,x,z);
    }
    for(i=0;++i<=n;)
    {
        if(f[i])
		    insert(0,i,0),
		    insert(i,0,0);
        fa[i]=i;
    }
    
    for(SPFA(0),dfs(),sort(e+1,e+1+len,myc),ans=0,tot=0,i=0;++i<=len;)
    {
        if(!e[i].fg)continue;
        int x=e[i].x,y=e[i].to;
        int _x=find(x),_y=find(y);
        if(_x!=_y)
        {
            fa[_x]=_y,++tot;
            ans+=e[i].v;
            if(tot==n-1)break;
        }
    }
    if(!ans)printf("impossible");
    else printf("%lld",ans);
    return 0;
 }

在這裏插入圖片描述


在這裏插入圖片描述

謝謝觀賞~~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章