/ Randle /

记录详情

Runtime Error


  
# 状态 耗时 内存占用
#1 Runtime Error 93ms 8.375 MiB
#2 Runtime Error 111ms 8.25 MiB
#3 Accepted 3ms 372.0 KiB
#4 Wrong Answer 166ms 2.75 MiB
#5 Runtime Error 103ms 8.445 MiB
#6 Accepted 2ms 380.0 KiB
#7 Wrong Answer 149ms 5.578 MiB
#8 Wrong Answer 186ms 4.605 MiB
#9 Wrong Answer 125ms 1.82 MiB
#10 Wrong Answer 229ms 1.75 MiB

代码

#include<iostream>
#include<cstdio>
#include<ctime>
#define INF 0x3f3f3f3f
#define N 20
using namespace std;
int dis[N],que[55],u[N],way[N][N][N],pre[N],juz[N][N],ndis[N][N];
int n,head,tail,ans=INF,sum,cnt;
void DFS(int x)
{
	cnt++;
	if(cnt>4000000) return ;
	int w;
	for(w=1;w<=n;w++)
	{
		//cout<<u[w]<<" ";
		if(u[w]==0) break;
		
	}
	//cout<<endl;
	//cout<<w<<" "<<x<<" "<<sum<<endl;
	if(w>n) 
	{
		//cout<<sum<<endl;
		ans=min(ans,sum+ndis[x][0]);
		//cout<<ans<<endl;
		return ;
	}
	for(int i=1;i<=n;i++)
	{
		if(!u[i])
		{
			//cout<<i<<endl;
			sum+=ndis[x][i];u[i]=1;
			if(sum>ans) 
			{
				u[i]=0;
				sum-=ndis[x][i];continue;
			}
			for(int j=1;j<=way[x][i][0];j++)
			{
				u[way[x][i][j]]=1;
			}
			DFS(i);
			for(int j=1;j<=way[x][i][0];j++)
			{
				u[way[x][i][j]]=0;
			}
			sum-=ndis[x][i];u[i]=0;
		}
	}
}
void SPFA(int x)
{
	
	for(int i=0;i<=n;i++)
	{
		dis[i]=INF;pre[i]=0;
	}
	head=0;tail=1;que[1]=x;u[x]=1;dis[x]=0;
	do
	{
		head++;
		head=(head-1)%50+1;
		int k=que[head];
		u[head]=0;
		for(int i=0;i<=n;i++)
		{
			if(dis[k]+juz[k][i]<dis[i])
			{
				dis[i]=dis[k]+juz[k][i];
				pre[i]=k;
				if(!u[i])
				{
					u[i]=1;
					tail++;
					tail=(tail-1)%50+1;
					que[tail]=i;
				}
			}
		}
	}while(head!=tail);
	for(int i=0;i<=n;i++)
	{
		ndis[x][i]=dis[i];
		if(i==x) continue;
		int w=i;
		while(pre[w]!=x)
		{
			w=pre[w];
			way[x][i][++way[x][i][0]]=w;
		}
		//way[x][i][++way[x][i][0]]=pre[w];
	}
	/*for(int i=0;i<=n;i++)
	{
		cout<<dis[i]<<" ";
	}
	cout<<endl;*/
}
int main()
{
	//freopen("a.txt","r",stdin);
	//freopen("b.txt","w",stdout);
	scanf("%d",&n);
	for(int i=0;i<=n;i++)
	{
		for(int j=0;j<=n;j++)
		{
			scanf("%d",&juz[i][j]);
		}
	}
	for(int i=0;i<=n;i++)
	{
		SPFA(i);
	}
	for(int i=1;i<=n;i++)
	u[i]=0;
	u[0]=1;
	DFS(0);
	printf("%d",ans);
	return 0;
}

信息

递交者
类型
递交
题目
送外卖
题目数据
下载
语言
C++
递交时间
2018-08-30 21:52:08
评测时间
2018-08-30 21:52:08
评测机
分数
20
总耗时
1171ms
峰值内存
8.445 MiB