AcWing 344. 观光之旅
原题链接
中等
作者:
M_Fish
,
2019-08-23 11:59:42
,
所有人可见
,
阅读 999
#include<bits/stdc++.h>
#define re register int
using namespace std;
int n,m,ans=0x3f3f3f3f,a[310][310],d[310][310],pos[310][310];
vector<int>path;
void get_path(int x,int y)
{
if(pos[x][y]==0) return;
get_path(x,pos[x][y]);
path.push_back(pos[x][y]);
get_path(pos[x][y],y);
}
int read()
{
int ans=0;
char w=' ',ch=getchar();
while(ch<'0' || ch>'9')
{
w=ch;
ch=getchar();
}
while(ch>='0' && ch<='9')
{
ans=(ans<<3)+(ans<<1);
ans+=ch-'0';
ch=getchar();
}
return w=='-'?-ans:ans;
}
int main()
{
n=read();m=read();
memset(a,0x3f,sizeof(a));
for(re i=1;i<=n;i++)
{
a[i][i]=0;
}
for(re i=1;i<=m;i++)
{
int x,y,z;
x=read();y=read();z=read();
a[x][y]=a[y][x]=min(a[x][y],z);
}
memcpy(d,a,sizeof(a));
for(re k=1;k<=n;k++)
{
for(re i=1;i<k;i++)
{
for(re j=i+1;j<k;j++)
{
if((long long)d[i][j]+a[j][k]+a[k][i]<ans)
{
ans=d[i][j]+a[j][k]+a[k][i];
path.clear();
path.push_back(i);
get_path(i,j);
path.push_back(j);
path.push_back(k);
}
}
}
for(re i=1;i<=n;i++)
{
for(re j=1;j<=n;j++)
{
if(d[i][j]>d[i][k]+d[k][j])
{
d[i][j]=d[i][k]+d[k][j];
pos[i][j]=k;
}
}
}
}
if(ans=0x3f3f3f3f)
{
cout<<"No solution.";
}
else
{
for(re i=0;i<path.size();i++)
{
cout<<path[i]<<" ";
}
}
return 0;
}
头像好评