POJ - 1734 Sightseeing trip
#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
const int N = 110, INF = 0x3f3f3f3f;
int d[N][N],g[N][N],pos[N][N];
vector<int> path;
void get_path(int i,int j)
{
if(pos[i][j]==0) return ;
int k=pos[i][j];
get_path(i,k);
path.push_back(k);
get_path(k,j);
}
int main()
{
int n,m;cin>>n>>m;
memset(g,0x3f,sizeof g);
for(int i=1;i<=n;i++) g[i][i]=0;
while(m--)
{
int a,b,c;cin>>a>>b>>c;
g[a][b]=g[b][a]=min(g[a][b],c);
}
int res=INF;
memcpy(d,g,sizeof d);
for(int k=1;k<=n;k++)
{
for(int i=1;i<k;i++)
for(int j=i+1;j<k;j++)
if((long long)d[i][j]+g[j][k]+g[k][i]<res)
{
res=d[i][j]+g[j][k]+g[k][i];
path.clear();
path.push_back(k);
path.push_back(i);
get_path(i,j);
path.push_back(j);
}
for(int i=1;i<=n;i++)
for(int 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(res==INF) cout<<"No solution."<<endl;
else
{
for(int i=0;i<(int)path.size();i++) cout<<path[i]<<" ";
cout<<endl;
}
return 0;
}