比赛 20120809 评测结果 AAATEEETTE
题目名称 周年纪念日 最终得分 30
用户昵称 Truth.Cirno 运行时间 3.664 s
代码语言 C++ 内存使用 21.77 MiB
提交时间 2012-08-09 12:40:49
显示代码纯文本
#include <fstream>
#include <cstdlib>
#include <memory.h>
using namespace std;

struct ints
{
	long long dis,x,y;
}mindis[100001];

long long p[100001],un[100001];
long long ystojh[100001],jhst[100001],jhen[100001],ysnext[100001],jhysnum[100001];
long long ava=0,tow[100001],way[200001],waydis[200001],waynext[200001],waylast[200001];
long long ava2=0,tow2[100001],way2[200001],waydis2[200001],waynext2[200001],waylast2[200001];
bool used[100001];

void swapints(ints& x,ints& y)
{
	ints temp;
	temp=x;
	x=y;
	y=temp;
}

void qqsort(long long l,long long r)
{
	long long ll,rr,temp;
	ll=l;
	rr=r;
	temp=mindis[rand()%(r-l+1)+l].dis;
	while (ll<=rr)
	{
		while (mindis[ll].dis<temp)
			ll++;
		while (temp<mindis[rr].dis)
			rr--;
		if (ll<=rr)
		{
			swapints(mindis[ll],mindis[rr]);
			ll++;
			rr--;
		}
	}
	if (l<rr)
		qqsort(l,rr);
	if (ll<r)
		qqsort(ll,r);
}

void addway(long long from,long long to,long long dis)
{
	if (tow[from]==0)
	{
		tow[from]=++ava;
		waylast[from]=ava;
		way[ava]=to;
		waydis[ava]=dis;
		return;
	}
	long long poi=waylast[from];
	waynext[poi]=++ava;
	waylast[from]=ava;
	way[ava]=to;
	waydis[ava]=dis;
}

void addway2(long long from,long long to,long long dis)
{
	if (tow2[from]==0)
	{
		tow2[from]=++ava2;
		waylast2[from]=ava2;
		way2[ava2]=to;
		waydis2[ava2]=dis;
		return;
	}
	long long poi=waylast2[from];
	waynext2[poi]=++ava2;
	waylast2[from]=ava2;
	way2[ava2]=to;
	waydis2[ava2]=dis;
}

void combineit(long long jha,long long jhb)
{
	long long pos;
	jhysnum[jha]=jhysnum[jha]+jhysnum[jhb];
	ysnext[jhen[jha]]=jhst[jhb];
	pos=jhst[jhb];
	while (pos)
	{
		ystojh[pos]=jha;
		pos=ysnext[pos];
	}
	jhen[jha]=jhen[jhb];
}

void combine(long long jha,long long jhb)
{
	if (jha==jhb)
		return;
	if (jhysnum[jha]>=jhysnum[jhb])
		combineit(jha,jhb);
	else
		combineit(jhb,jha);
}

void make(long long pos,long long cost)
{
	used[pos]=true;
	un[pos]=cost;
	long long poi=tow2[pos];
	while (poi)
	{
		if (!used[way2[poi]])
			make(way2[poi],cost+waydis2[poi]);
		poi=waynext2[poi];
	}
}

int main(void)
{
	ifstream input("anniversary.in");
	ofstream output("anniversary.out");
	long long i,j,n,m,a,b,c,jhnum,maxdis=0,cost=0,mincost=-1,minpos;
	input>>n>>m;
	for (i=1;i<=n;i++)
	{
		ystojh[i]=i;
		jhst[i]=i;
		jhen[i]=i;
		jhysnum[i]=1;
	}
	for (i=1;i<=n;i++)
		input>>p[i];
	for (i=1;i<=m;i++)
	{
		input>>a>>b>>c;
		addway(a,b,c);
		addway(b,a,c);
		mindis[i].dis=c;
		mindis[i].x=a;
		mindis[i].y=b;
	}
	qqsort(1,m);
	jhnum=n;
	for (i=1;i<=m;i++)
	{
		if (ystojh[mindis[i].x]!=ystojh[mindis[i].y])
		{
			addway2(mindis[i].x,mindis[i].y,mindis[i].dis);
			addway2(mindis[i].y,mindis[i].x,mindis[i].dis);
			if (maxdis<mindis[i].dis)
				maxdis=mindis[i].dis;
			cost+=mindis[i].dis;
			combine(ystojh[mindis[i].x],ystojh[mindis[i].y]);
			jhnum--;
			if (jhnum==1)
				break;
		}
	}
	output<<cost<<' '<<maxdis<<endl;
	for (i=1;i<=n;i++)
	{
		cost=0;
		memset(used,0,sizeof(used));
		make(i,0);
		for (j=1;j<=n;j++)
			cost+=un[j]*p[j];
		if (mincost>cost||mincost==-1)
		{
			mincost=cost;
			minpos=i;
		}
	}
	output<<minpos<<' '<<mincost<<endl;
	return(0);
}