Problem:
A Long Ride on a Railway
Travelling by train is fun and exciting. But more than that indeed.  Young
challenging boys often tried to purchase the longest single tickets and to
single ride the longest routes of various railway systems.  Route planning was
like solving puzzles. However, once assisted by computers, and supplied with
machine readable databases, it is not any more than elaborating or hacking
on the depth-first search algorithms.
Map of a railway system is essentially displayed in the form of a graph. (See
the Figures below.) The vertices (i.e. points) correspond to stations and the
edges (i.e. lines) correspond to direct routes between stations. The station
names (denoted by positive integers and shown in the upright letters in the
Figures) and direct route distances (shown in the slanted letters) are also
given.
The problem is to find the length of the longest path and to form the
corresponding list of stations on the path for each system.  The longest of
such paths is one that starts from one station and, after travelling many
direct routes as far as possible without passing any direct route more than
once, arrives at a station which may be the starting station or different one
and is longer than any other sequence of direct routes.  Any station may be
visited any number of times in travelling the path if you like.
Input
The graph of a system is defined by a set of lines of integers of the
form:
ns
nl
s
1,1
s
1,2
d
1
s
2,1
s
2,2
d
2
...
s
nl
,1
s
nl
,2
d
nl
Here 1 <=
ns
<= 10 is the number of stations (so, station names are 1, 2,
...,
ns
), and 1 <=
nl
<= 20 is the number of direct routes in this
graph.
s
i
,1
and
s
i
,2
(
i
= 1, 2, ...,
nl
) are station names at the both ends of the
i
-th direct route.
d
i
>= 1 (
i
= 1, 2, ...,
nl
) is the direct route distance between two different stations
s
i
,1
and
s
i
,2
.
It is also known that there is at most one direct route between any pair of
stations, and all direct routes can be travelled in either directions.  Each
station has at most four direct routes leaving from it.
The integers in an input line are separated by at least one white space (i.e. a
space character (ASCII code 32) or a tab character (ASCII code 9)), and possibly
preceded and followed by a number of white spaces.
The whole input of this problem consists of a few number of sets each of which
represents one graph (i.e. system) and the input terminates with two integer
0
's in the line of next
ns
and
nl
.
Output
Paths may be travelled from either end; loops can be traced clockwise or
anticlockwise. So, the station lists for the longest path for Figure A are
multiple like (in lexicographic order):
2 3 4 5
5 4 3 2
For Figure B, the station lists for the longest path are also multiple like
(again in lexicographic order):
6 2 5 9 6 7
6 9 5 2 6 7
7 6 2 5 9 6
7 6 9 5 2 6
Yet there may be the case where the longest paths are not unique. (That is,
multiple independent paths happen to have the same longest distance.) To make
the answ
...(truncated)

Submitted solutions:

code_snippet: #include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int,int> PI;
const double EPS=1e-6;
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define F first
#define S second
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define SZ(a) (int)((a).size())
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) a.rbegin(),a.rend()
#define FLL(a,b) memset((a),b,sizeof(a))
#define CLR(a) FLL(a,0)
#define declare(a,it) __typeof(a) it=(a)
#define FOR(it,a) for(declare(a.begin(),it);it!=a.end();++it)
#define FORR(it,a) for(declare(a.rbegin(),it);it!=a.rend();++it)
template<class T,class U> ostream& operator<< (ostream& o, const pair<T,U>& v){return o << "(" << v.F << ", " << v.S << ")";}
template<class T> ostream& operator<< (ostream& o, const vector<T>& v){o << "{";rep(i,SZ(v)) o << (i?", ":"") << v[i];return o << "}";}
int dx[]={0,1,0,-1,1,1,-1,-1};
int dy[]={1,0,-1,0,-1,1,1,-1};
int s2i(string& a){stringstream ss(a);int r;ss>>r;return r;}
int geti(){int n;scanf("%d", &n);return n;}

int ns,nl;
int s[20],t[20], d[20];

int memo[10][1<<20];
int rec(int v,int st){
  if(memo[v][st] >= 0)
    return memo[v][st];
  int& ret = memo[v][st] = 0;

  for(int i = 0; i < nl; ++i){
    if((st>>i)&1) continue;
    if(v == s[i])
      ret = max(ret, rec(t[i], st|(1<<i)) + d[i]);
    if(v == t[i])
      ret = max(ret, rec(s[i], st|(1<<i)) + d[i]);
  }
  
  return ret;
}

int getpath(int v,int st){
  cout << v+1;
  if(memo[v][st] == 0){
    cout << endl;
    return memo[v][st];
  }
  cout << " ";
  int ret = memo[v][st];
  int ne = ns;
  int nst;
  
  for(int i = 0; i < nl; ++i){
    if((st>>i)&1) continue;
    if(v == s[i] && rec(t[i], st|(1<<i)) + d[i] == ret && t[i] < ne){
      ne = t[i];
      nst = st | (1<<i);
    }

    if(v == t[i] && rec(s[i], st|(1<<i)) + d[i] == ret && s[i] < ne){
      ne = s[i];
      nst = st | (1<<i);
    }    
  }

  getpath(ne,nst);
  
  return ret;
}


int main(int argc, char *argv[])
{
  while(cin >> ns >> nl && ns){
    for(int i = 0; i < nl; ++i){
      cin >> s[i] >> t[i] >> d[i];
      --s[i], --t[i];
    }
    memset(memo, -1, sizeof(memo));
    int ma = 0;
    for(int i = 0; i < ns; ++i)
      ma = max(ma, rec(i, 0));
    cout << ma << endl;
    for(int i = 0; i < ns; ++i){
      if(rec(i,0) == ma){
        getpath(i,0);
        break;
      }
    }
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 42132
final_rank: 4

code_snippet: //20
#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;

int ns,nl;
int d[11][11];
int ln[11][11];
vector<int> mv,cv;
int m;
bool p[1<<20][11];

void dfs(int v,int ds,int l){
  cv.push_back(v);
  for(int i=1;i<=ns;i++){
    int ll=l|1<<ln[v][i];
    if(d[v][i]&&!p[ll][i]++){
      int cs=d[v][i];
      d[v][i]=d[i][v]=0;
      dfs(i,ds+cs,ll);
      d[v][i]=d[i][v]=cs;
    }
  }
  if(ds>m){
    m=ds;
    mv=cv;
  }
  cv.pop_back();
}

int main(){
  while(cin>>ns>>nl,ns|nl){
    fill(d[0],d[11],0);
    for(int i=0;i<nl;i++){
      int s,t,dd;
      cin>>s>>t>>dd;
      d[s][t]=d[t][s]=dd;
      ln[s][t]=ln[t][s]=i;
    }
    fill(p[0],p[(1<<20)],false);
    m=0;
    for(int i=1;i<=ns;i++){
      dfs(i,0,0);
    }
    cout<<m<<endl;
    for(int i=0;i<mv.size();i++){
      cout<<mv[i]<<((i==mv.size()-1)?'\n':' ');
    }
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 12476
final_rank: 2

code_snippet: #include <iostream>
#include <vector>
#include <map>

using namespace std;

#define dump(n) cerr<<"# "<<#n<<"="<<(n)<<endl
#define rep(i,n) for(int i=0;i<n;i++)

typedef vector<int> vi;
typedef vector<vi> vvi;

const int INFTY=1<<29;

pair<vi,int> solve(int v,vvi& dist,map<pair<vvi,int>,pair<vi,int> >& memo)
{
	map<pair<vvi,int>,pair<vi,int> >::iterator it=memo.find(make_pair(dist,v));
	if(it!=memo.end())
		return it->second;
	
	int n=dist.size();
	pair<vi,int> res;
	rep(i,n){
		if(dist[v][i]==INFTY)
			continue;
		int d=dist[v][i];
		dist[v][i]=dist[i][v]=INFTY;
		pair<vi,int> temp=solve(i,dist,memo); temp.second+=d;
		if(temp.second>res.second || temp.second==res.second && temp.first<res.first)
			res=temp;
		dist[v][i]=dist[i][v]=d;
	}
	res.first.insert(res.first.begin(),v);
	return memo[make_pair(dist,v)]=res;
}

int main()
{
	for(int n,m;cin>>n>>m,n|m;){
		vvi dist(n,vi(n,INFTY));
		rep(i,m){
			int s,d,w; cin>>s>>d>>w;
			dist[s-1][d-1]=dist[d-1][s-1]=w;
		}
		map<pair<vvi,int>,pair<vi,int> > memo;
		pair<vi,int> res;
		rep(i,n){
			pair<vi,int> temp=solve(i,dist,memo);
			if(temp.second>res.second || temp.second==res.second && temp.first<res.first)
				res=temp;
		}
		cout<<res.second<<endl;
		rep(i,res.first.size())
			cout<<(res.first[i]+1)<<(i==res.first.size()-1?'\n':' ');
	}
	
	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 1836
final_rank: 3

code_snippet: #include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;

int ans;
vector<int> ansv;
vector<vector<int> > v;

void dfs(int prev, int next, int a, vector<int> vi, set<pair<int,int> > s){
  if(v[prev][next] == -1 || s.find(pair<int,int>(prev,next)) != s.end()) return;
  vi.push_back(next);
  s.insert(pair<int,int>(prev,next));
  s.insert(pair<int,int>(next,prev));
  a += v[prev][next];
  if(ans < a){
    ans = a;
    ansv = vi;
  }
  for(int i=0;i<v.size();i++) dfs(next,i,a,vi,s);
}

int main(){
  int n,m;
  while(cin >> n >> m && n && m){
    ans = 0;
    ansv.clear();
    v.assign(n,vector<int>(n,-1));
    for(int i=0;i<m;i++){
      int a,b;
      cin >> a >> b;
      a--;
      b--;
      cin >> v[a][b];
      v[b][a] = v[a][b];
    }

    for(int i=0;i<n;i++){
      vector<int>vi;
      set<pair<int,int> > s;
      vi.push_back(i);
      for(int j=0;j<n;j++) dfs(i,j,0,vi,s);
    }
    cout << ans << endl;
    cout << ansv[0] + 1;
    for(int i=1;i<ansv.size();i++) cout << " " << ansv[i] + 1;
    cout << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 920
final_rank: 5

code_snippet: #include<iostream>
#include<queue>
#include<vector>
#include<algorithm>

using namespace std;

struct node{
  vector<int> con;
  vector<int> cost;
  vector<int> e_id;
};

typedef vector<node> Graph;

struct State{
  int now;
  int dist;
  int used;
  State(int n, int d, int u):now(n),dist(d),used(u){}
  bool visited(int i){return (bool)(used&(1LL<<i));}
  void setUse(int i){used|=(1LL<<i);}
  vector<int> hist;
  bool operator>(const State &t)const{
    return dist > t.dist;
  }
};

pair<int,vector<int> > solve(int st, Graph &G){
  priority_queue<State,vector<State>,greater<State> > Q;
  State init(st,0,0);
  init.hist.push_back(st+1);
  Q.push(init);
  pair<int, vector<int> > ret;
  while(!Q.empty()){
    State nst = Q.top();
    Q.pop();
    int nowid = nst.now;

    if(ret.first<nst.dist){
      ret=make_pair(nst.dist,nst.hist);
    }else if(ret.first==nst.dist){
      if(nst.hist<ret.second){
	ret.second = nst.hist;
      }
    }

    for(int i = 0; i < (int)G[nowid].con.size(); ++i){
      State next(G[nowid].con[i], nst.dist + G[nowid].cost[i], nst.used);
      if(!nst.visited(G[nowid].e_id[i])){
	next.setUse(G[nowid].e_id[i]);
	next.hist=nst.hist;
	next.hist.push_back(G[nowid].con[i]+1);
	Q.push(next);
      }
    }
  }
  return ret;
}

int main()
{
  while(true){
    int ns,nl;
    cin >> ns >> nl;
    if(ns==0&&nl==0)break;
    Graph G(ns);
    for(int i = 0; i < nl; ++i){
      int src,dst,w;
      cin >> src >> dst >> w;
      G[src-1].con.push_back(dst-1);
      G[src-1].cost.push_back(w);
      G[src-1].e_id.push_back(i);
      G[dst-1].con.push_back(src-1);
      G[dst-1].cost.push_back(w);
      G[dst-1].e_id.push_back(i);
    }
    pair<int, vector<int> > res;
    for(int i = 0; i < ns; ++i){
      pair<int, vector<int> > tmp = solve(i,G);
      if(res.first < tmp.first){
	res = tmp;
      }else if( res.first == tmp.first ){
	if( tmp.second < res.second )
	  res.second = tmp.second;
      }
    }
    cout << res.first << endl;
    for(int i = 0; i < (int)res.second.size(); ++i){
      if(i>0)cout << ' ';
      cout << res.second[i];
    }
    cout << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 0
final_rank: 1