/ Randle /

记录详情

Accepted


  
# 状态 耗时 内存占用
#1 Accepted 4ms 2.199 MiB
#2 Accepted 15ms 30.25 MiB
#3 Accepted 14ms 26.25 MiB
#4 Accepted 270ms 156.25 MiB
#5 Accepted 271ms 156.375 MiB
#6 Accepted 304ms 156.25 MiB
#7 Accepted 184ms 146.25 MiB
#8 Accepted 98ms 70.285 MiB
#9 Accepted 110ms 106.25 MiB
#10 Accepted 295ms 156.375 MiB

代码

#include<bits/stdc++.h>
#define inf 1000000007
#define N 24
using namespace std;
long long  e[N],cnt[1050000],d[N][N],f[2][N][1050000];
int n,m,n1,n2;
inline const void read(int &a)
{
	a=0;
	char c=getchar();
	while(c<'0'||c>'9')c=getchar();
	while(c<='9'&&c>='0')
	{
		a=(a<<1)+(a<<3)+c-'0';
		c=getchar();
	}
}
int main()
{
	int x,y,z;
	read(n);read(m);
	memset(e,0,sizeof(e));
	memset(d,0,sizeof(d));
	e[2]=1;
	for (int i=3;i<=n;i++)
		e[i]=e[i-1]<<1;
	for (int i=1;i<=e[n]-1;i++)
		for (int j=i;j>0;j>>=1)
			cnt[i]+=j&1;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
		d[i][j]=inf*(i!=j);
	for (int i=1;i<=m;i++)
	{
		read(x);read(y);read(z);
		x++;
		y++;
		d[x][y]=d[y][x]=z;
	}
	if(n==3)
	{
		cout<<(d[1][2]+d[2][3])*2<<endl;
		return 0;
	}
	for (int k=1;k<=n;k++)
	for (int i=1;i<=n;i++)
	for (int j=1;j<=n;j++)
	if (d[i][k]+d[k][j]<d[i][j]) d[i][j]=d[i][k]+d[k][j];
	n1=(n-2)/2;
	n2=n-2-n1;
	for (int q=0;q<=1;q++)
	{
		for (int i=2;i<=n;i++)
		for (int j=0;j<=e[n]-1;j++)
		f[q][i][j]=inf;
		if(q==0)
		for (int i=2;i<n;i++)
		f[0][i][e[i]]=d[1][i];
		if (q==1)
		for (int i=2; i<n; i++)
		f[1][i][e[i]]=d[n][i];
		for (int j=1; j<e[n]; j++)
		if (cnt[j]<n2)
		for (int i=2; i<n; i++)
		if (f[q][i][j] < inf)
		for (int k=2; k<n; k++)
		if (f[q][i][j]+d[i][k] < f[q][k][j|e[k]]) f[q][k][j|e[k]]=f[q][i][j]+d[i][k];
	}
	int ans=inf;
	for (int ast=0; ast<e[n]; ast++)
	if(cnt[ast]==n1)
	{
		int x=inf;
		for (int i=2; i<n; i++)
		if (ast&e[i])
		for (int j=2; j<n; j++)
		if (!(ast&e[j]))
		if (f[0][i][ast]+d[i][j]+f[1][j][e[n]-1-ast]<x) 
			x=f[0][i][ast]+d[i][j]+f[1][j][e[n]-1-ast];
		for (int i=2; i<n; i++)
		if (ast&e[i])
		for (int j=2; j<n; j++)
		if (!(ast&e[j]))
		if (x+f[1][i][ast]+d[i][j]+f[0][j][e[n]-1-ast]<ans)
			ans=f[1][i][ast]+d[i][j]+f[0][j][e[n]-1-ast]+x;
	}
	cout<<ans<<endl;
	return 0;
}

信息

递交者
类型
递交
题目
香子兰 T3
题目数据
下载
语言
C++
递交时间
2017-11-09 10:20:14
评测时间
2017-11-09 10:20:14
评测机
分数
100
总耗时
1569ms
峰值内存
156.375 MiB