Problem:
温泉旅行
温泉好きのたけしさんは、次の長期休暇を利用してとある温泉地への旅行を計画しています。移動は長距離バスを乗り継ぎ、なるべくお金をかけずに目的地へたどり着きたいと思っています。貯金があるとはいえ、資金に心許ないたけしさんは、おじいさんに相談することにしました。計画を聞いて感心したおじいさんは、たけしさんに特別な切符を渡しました。
その切符は、長距離バスの連続した2区間を1回だけ無料で乗れるというものでした。使いようによってはかなりの移動費削減が見込めますが、より大きな効果を発揮させるためにはしっかりした計画を練る必要があります。
出発地と目的地、及び中継地点が合わせて
n
個、2つの地点を結ぶ路線が
m
個与えられます。各地点にはそれぞれ 1 から
n
までの数字が割り振られています。出発地は 1、目的地は
n
です。路線の情報は、その路線が結ぶ2つの地点
a
と
b
、及びその料金
c
で表されます。特別な切符の効力により、任意の地点から、一度だけ連続した2つの路線を料金0で通過することができます。ただし、途中で目的地を通過しても、目的地にたどり着いたことにはなりません。
出発地、目的地、及び中継地点の総数
n
と路線の数
m
、各路線の情報を入力とし、料金の最小値を出力するプログラムを作成してください。ただし、必ず出発地から目的地へと到達する経路が存在するものとします。
入力
複数のデータセットが与えられます。入力の終わりはゼロふたつの行で示されます。各データセットは以下の形式で与えられます。
n
m
a
1
b
1
c
1
a
2
b
2
c
2
:
a
m
b
m
c
m
1行目に出発地、目的地、及び中継地点の総数
n
(2 ≤
n
≤ 100)と路線の数
m
(1 ≤
m
≤ 300)が与えられます。続く
m
行に各路線の情報
a
i
,
b
i
,
c
i
(1 ≤
c
i
≤ 1000) が与えられます。
データセットの数は 40 を超えません。
出力
入力データセットごとに、料金の最小値を1行に出力します。
入力例
2 1
1 2 5
3 2
1 2 5
2 3 5
6 9
1 2 7
1 3 9
1 5 14
2 3 10
2 4 15
3 4 11
3 5 2
4 5 9
4 6 8
0 0
出力例
5
0
7

Submitted solutions:

code_snippet: #include<bits//stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define req(i,n) for(int i = 1;i <=  n; i++)
#define rrep(i,n) for(ll i = n-1;i >= 0;i--)
#define ALL(obj) begin(obj), end(obj)
#define RALL(a) rbegin(a),rend(a)
typedef long long int ll;
typedef long double ld;
const int MAX = 510000;
const ll MOD = 1000000007;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
int main(void){
    int n,m;
    while(cin >>n >>m,n){
        vector<vector<int>> d(n,vector<int>(n,1e9));
        rep(i,n) d[i][i]=0;
        int a,b,c;
        rep(i,m){
            cin >>a>>b>>c;a--;b--;
            d[a][b] = c;d[b][a] = c;
        }auto g = d;
        rep(i,n) rep(j,n) rep(k,n) chmin(d[j][k],d[j][i]+d[i][k]);
        int ans = d[0][n-1];
        rep(i,n) rep(j,n) rep(k,n){
            if(i==j||j==k||i==k) continue;
            if(g[i][j]==1e9||g[j][k]==1e9) continue;
            chmin(ans,d[0][i]+d[k][n-1]);
        }cout << ans <<endl;
    }
}
accuracy: 1
time_ms: 40
memory_kb: 3248
final_rank: 14

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int inf = 1e9;

int main(){
    while(1){
        int n,m;
        cin >> n >> m;
        if(n == 0) break;
        
        vector<vector<int>> g(n, vector<int>(n, inf));
        for(int i=0; i<n; i++){
            g[i][i] = 0;
        }
        for(int i=0; i<m; i++){
            int a,b,c;
            cin >> a >> b >> c;
            a--; b--;
            g[a][b] = min(g[a][b], c);
            g[b][a] = min(g[b][a], c);
        }
        auto d = g;
        for(int k=0; k<n; k++){
            for(int i=0; i<n; i++){
                for(int j=0; j<n; j++){
                    d[i][j] = min(d[i][j], d[i][k]+d[k][j]);
                }
            }
        }
        int ans = d[0][n-1];
        for(int i=0; i<n; i++){
            for(int j=0; j<n; j++){
                for(int k=0; k<n; k++){
                    if(i==j or j==k or k==i) continue;
                    if(g[i][j]==inf or g[j][k]==inf) continue;
                    ans = min(ans, d[0][i]+d[k][n-1]);
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3236
final_rank: 13

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

using namespace std;

#define rep(i,n) for(int i=0;i<(n);++i)
#define reps(i,n) for(int i=1;i<=(n);++i)
#define all(x) (x).begin(),(x).end()
#define Fixed fixed << setprecision(12)
#define int int_fast64_t
using pii = pair<int,int>;
constexpr int INF  = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr int mod1 = 1e9+7; 
constexpr int mod2 = 998244353;
 
template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }
template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }
 
template <class T> using min_heap = priority_queue<T,vector<T>,greater<T> >;
template <class T> using max_heap = priority_queue<T>;
template <class A, class B> using umap = unordered_map<A,B>;

inline int updiv(int a,int b){ return (a + b - 1) / b; }
 
constexpr int dx[] = {1,0,-1,0,1,1,-1,-1};
constexpr int dy[] = {0,-1,0,1,1,-1,-1,1};


signed main(){
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout.setf(ios_base::fixed);
    cout.precision(12);

    int n, m;
    while(cin >> n >> m, n){
        vector<vector<pii> > g(n+1);
        rep(i,m){
            int a, b, c;
            cin >> a >> b >> c;
            g[a].emplace_back(b, c);
            g[b].emplace_back(a, c);
        }

        vector<vector<int> > cost(n+1, vector<int>(2, LINF));
        priority_queue<tuple<int, int, int> > que;
        cost[1][0] = 0;
        que.emplace(0, 1, 0);
        while(!que.empty()){
            int now_node, now_cost, st;
            tie(now_cost, now_node, st) = que.top();
            que.pop();
            if(st != 2 && cost[now_node][st] < now_cost){
                continue;
            }
            for(auto &&e : g[now_node]){
                int to, c;
                tie(to, c) = e;
                if(st == 2){
                    que.emplace(now_cost, to, 1);
                    chmin(cost[to][1], now_cost);
                }else if(st == 1 && chmin(cost[to][1], cost[now_node][1] + c)){
                    que.emplace(cost[to][1], to, 1);
                }else if(st == 0){
                    if(chmin(cost[to][0], cost[now_node][0] + c)){
                        que.emplace(cost[to][0], to, 0);
                    }
                    que.emplace(now_cost, to, 2);
                }
            }
        }
        cout << min(cost[n][0], cost[n][1]) << '\n';
    }

    return (0);
}
accuracy: 1
time_ms: 50
memory_kb: 3208
final_rank: 15

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

using namespace std;

int N, M;
int MapData[101][101], DpMoney[2][101];
bool Used[2][101];
const int INF = 100000000;


void solve() {
	int u, v;

	for (int i = 0; i < 2; ++i) {
		fill(DpMoney[i], DpMoney[i] + 101, INF);
		fill(Used[i], Used[i] + 101, false);
	}

	DpMoney[0][1] = 0;
	while (true) {
		u = -1;
		for (int i = 0; i < 2; ++i) {
			for (int j = 1; j <= N; ++j) {
				if (!Used[i][j] && (u == -1 || DpMoney[i][j] < DpMoney[u][v])) {
					u = i;
					v = j;
				}
			}
		}
		if (u == -1) {
			break;
		}
		Used[u][v] = true;
		//cout << "u(ticket):" << u << " v(dest):" << v << endl;

		for (int k = 1; k <= N; ++k) {
			DpMoney[u][k] = min(DpMoney[u][k], DpMoney[u][v] + MapData[v][k]);
			//cout << "u(ticket):" << u << "  k(now):" << k  << " money:" << DpMoney[u][k] << endl;
			if (u == 1) {
				continue;
			}
			for (int next = 1; next <= N; ++next) {
				if (MapData[v][k] == INF || MapData[k][next] == INF) {
					continue;
				}
				DpMoney[1][next] = min(DpMoney[1][next], DpMoney[0][v] + 0);
				//cout << "k(now):" << k << "  next:" << next << " money:" << DpMoney[1][next] << endl;
			}
		}
	}
}

int main() {
	int a, b, c;

	while (cin >> N >> M, N) {
		for (int i = 0; i < 101; ++i) {
			fill(MapData[i], MapData[i] + 101, INF);
		}
		for (int i = 0; i < M; ++i) {
			cin >> a >> b >> c;
			MapData[a][b] = c;
			MapData[b][a] = c;
		}
		solve();
		cout << min(DpMoney[0][N], DpMoney[1][N]) << endl;
	}

	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3136
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define INF 200000

int n, m;
bool is_edge[110][110];
int d[110][110];

void warshall_floyd() {
    for (int k=0; k<n; k++) {
        for (int i=0; i<n; i++) {
            for (int j=0; j<n; j++) {
                d[i][j] = min(d[i][j], d[i][k]+d[k][j]);
            }
        }
    }
}

int main() {
    while (true) {
        cin >> n >> m;
        
        if (n==0 && m==0) {
            break;
        }
        
        for (int i=0; i<n; i++) {
            for (int j=0; j<n; j++) {
                is_edge[i][j] = false;
                d[i][j] = (i==j) ? 0 : INF;
            }
        }
        
        for (int i=0; i<m; i++) {
            int a, b, c; cin >> a >> b >> c;
            is_edge[a-1][b-1] = true, is_edge[b-1][a-1] = true;
            d[a-1][b-1] = c, d[b-1][a-1] = c;
        }
        
        warshall_floyd();
        
        int ans = d[0][n-1];
        
        for (int i=0; i<n; i++) {
            for (int j=i+1; j<n; j++) {
                for (int k=0; k<n; k++) {
                    if (is_edge[k][i] && is_edge[k][j]) {
                        ans = min({ans, d[0][i]+d[j][n-1], d[0][j]+d[i][n-1]});
                        break;
                    }
                }
            }
        }
        
        cout << ans << endl;
    }
}
accuracy: 1
time_ms: 20
memory_kb: 3160
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<ll, pair<ll, ll> > P3;

const ll MOD = ll(1e9 + 7);
const ll LLINF = LLONG_MAX;
const int IINF = INT_MAX;
const int MAX_N = int(3e5) + 5;
const double EPS = 1e-8;
const int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
string dir = "RDLU";
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define SORT(v) sort((v).begin(), (v), end())
#define SORTR(v) sort((v).rbegin(), (v), rend())
#define ALL(v) (v).begin(), (v).end()

int main() {
    while (1) {
        int n, m, d[105][105]{};
        vector<int> g[105];
        cin >> n >> m;
        if(n==0)break;
        REP(i, n) fill(d[i], d[i] + n, IINF / 2);
        REP(i,n) d[i][i] = 0;
        REP(i, m) {
            int a, b, c;
            cin >> a >> b >> c;
            a--;
            b--;
            d[a][b] = c;
            d[b][a] = c;
            g[a].push_back(b);
            g[b].push_back(a);
        }
        REP(k, n) REP(i, n) REP(j, n) {
            d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
        }
        int ans = d[0][n-1];
        REP(k, n) REP(j, g[k].size()) REP(i, j) {
            ans = min(ans,d[0][g[k][i]]+d[g[k][j]][n-1]);
            ans = min(ans,d[0][g[k][j]]+d[g[k][i]][n-1]);
        }
        cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3196
final_rank: 6

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int n,m;
vector<int>G[100];
int D[100][100];
main()
{
	while(cin>>n>>m,n)
	{
		for(int i=0;i<n;i++)G[i].clear();
		for(int i=0;i<n;i++)for(int j=0;j<n;j++)if(i!=j)D[i][j]=1e9;
		for(int i=0;i<m;i++)
		{
			int a,b,c;cin>>a>>b>>c;a--,b--;
			G[a].push_back(b);
			G[b].push_back(a);
			D[a][b]=D[b][a]=c;
		}
		for(int k=0;k<n;k++)for(int i=0;i<n;i++)for(int j=0;j<n;j++)
			D[i][j]=min(D[i][j],D[i][k]+D[k][j]);
		int ans=D[0][n-1];
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<G[i].size();j++)
			{
				for(int k=0;k<G[i].size();k++)
				{
					if(j==k)continue;
					ans=min(ans,D[0][G[i][j]]+D[G[i][k]][n-1]);
				}
			}
		}
		cout<<ans<<endl;
	}
}
accuracy: 1
time_ms: 10
memory_kb: 3176
final_rank: 5

code_snippet: #include <bits/stdc++.h>
#define range(i, a, b) for(int i = (a); i < (b); i++)
#define rep(i, a) range(i, 0, a)
using namespace std;

const int INF = 1e9;

int main() {
    int n, m;
    while (cin >> n >> m, n) {
        vector <vector <int>> G(n, vector <int> (n, INF));
        rep (i, m) {
            int a, b, c;
            cin >> a >> b >> c;
            a--; b--;
            G[a][b] = c;
            G[b][a] = c;
        }
        //state -> dist, pre, now, usedTicket
        using state = tuple <int, int, int, bool>;
        priority_queue <state> que;
        vector <vector <vector <int>>> dp(n, vector <vector<int>> (n, vector <int> (2, INF)));

        que.emplace(0, 0, 0, 0);
        dp[0][0][0] = 0;
        dp[0][0][1] = 0;

        while(!que.empty()) {
            state p = que.top(); que.pop();
            int dist, pre, now; bool usedTicket;
            tie(dist, pre, now, usedTicket) = p;

            rep (to, n) {
                if (now == to || G[now][to] == INF) continue;
                //チケット使わない
                if (dp[to][now][usedTicket] > dist + G[now][to]) {
                    dp[to][now][usedTicket] = dist + G[now][to];
                    que.emplace(dist + G[now][to], now, to, usedTicket);
                }
                //チケット使う
                if (!usedTicket && G[pre][now] != INF) {
                    if (dp[to][now][true] > dist - G[pre][now]) {
                        dp[to][now][true] = dist - G[pre][now];
                        que.emplace(dist - G[pre][now], now, to, true);
                    }
                }
            }
        }
        int ans = INF;
        rep (i, n) rep (j, 2) {
            ans = min(ans, dp[n - 1][i][j]);
        }
        cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 290
memory_kb: 3420
final_rank: 18

code_snippet: #include "bits/stdc++.h"
#define int long long
#define loop(i, a, b) for(int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define all(a) (a).begin(),(a).end()
using namespace std;
const int MOD = 1e9 + 7, INF = 1e9;
using vi = vector <int>;
using vvi = vector <vi>;
//g++ -std==c++14
struct edge {int to, cost;};
vector <edge> G[100010];
using P = pair<int , int>;
int V;
int d[100010];

void dijkstra(int s){
    priority_queue <P, vector<P>, greater<P> > que;
    fill(d, d + V, 1000000000);
    d[s] = 0;
    que.push(P(0 , s));
    
    while(!que.empty()){
        P p = que.top(); que.pop();
        int v = p.second;
        if(d[v] < p.first)  continue;
        for(int i = 0; i < G[v].size(); i++){
            edge e = G[v][i];
            if(d[e.to] > d[v] + e.cost){
                d[e.to] = d[v] + e.cost;
                que.push(P(d[e.to], e.to));
            }
        }
    }
}

signed main(){
	int m;
	while(cin >> V >> m, V){
		int a, b, c;
		rep(i, m){
			cin >> a >> b >> c;
			a--; b--;
			G[a].push_back({b, c});
			G[b].push_back({a, c});
		}
		struct add{int kokkara, kokomade;};
		vector <add> karen;
		rep(i, V){
			rep(j, G[i].size()){
				int va = G[i][j].to;
				rep(k, G[va].size()){
					if(G[va][k].to == i)continue;
					karen.push_back({i, G[va][k].to});
				}
			}
		}
		dijkstra(0);
		int mini = d[V - 1];
		rep(i, karen.size()){
			G[karen[i].kokkara].push_back({karen[i].kokomade, 0});
			dijkstra(0);
			G[karen[i].kokkara].pop_back();
			mini = min(mini, d[V - 1]);
		}
		cout << mini << endl;
		rep(i, V)
			G[i].clear();
	}
}
accuracy: 1
time_ms: 110
memory_kb: 5556
final_rank: 17

code_snippet: #include<bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;
int n,m,a,b,c,d[101][101];
bool t[101][101];
main(){
  while(cin>>n>>m,n){
    int ans=1e9;
    r(i,n)r(j,n)d[i][j]=i==j?0:1e8;
    memset(t,0,sizeof(t));
    r(i,m){
      cin>>a>>b>>c;a--;b--;
      d[a][b]=d[b][a]=c;
      t[a][b]=t[b][a]=1;
    }
    r(o,n)r(i,n)r(j,n)d[i][j]=min(d[i][j],d[i][o]+d[o][j]);
    r(o,n)r(i,n)r(j,n)if(t[i][o]&&t[o][j])ans=min(ans,d[0][i]+d[j][n-1]);
    cout<<ans<<endl;
  }
}
accuracy: 1
time_ms: 40
memory_kb: 3084
final_rank: 12

code_snippet: #include <bits/stdc++.h>
#define INF 1e+9
using namespace std;

struct edge{ int to,cost; };
typedef pair<int,int> P;

int main(){
	int n,m;
	while(cin >> n >> m,n){
		int mi = INF,d[100];
		vector<edge> G[100];
		for(int i = 0;i < m;i++){
			int a,b,c;
			cin >> a >> b >> c; a--; b--;
			G[a].push_back({b,c});
			G[b].push_back({a,c});
		}
		if(n == 2){
			cout << G[0][0].cost << endl;
			continue;
		}
		for(int i = 0;i < n;i++){
			for(int j = i + 1;j < n;j++){
				set<int> st;
				bool flag = false;
				for(int k = 0;k < G[i].size();k++) st.insert(G[i][k].to);
				for(int k = 0;k < G[j].size();k++){
					if(st.count(G[j][k].to)) flag = true;
				}
				if(!flag) continue;
				vector<edge> G2[100];
				for(int k = 0;k < n;k++){
					d[k] = INF;
					G2[k] = G[k];
				}
				G2[i].push_back({j,0});
				G2[j].push_back({i,0});
				d[0] = 0;
				priority_queue<P,vector<P>,greater<P> > que;
				que.push(P(0,0));
				while(!que.empty()){
					P p = que.top();que.pop();
					int v = p.second;
					if(d[v] < p.first) continue;
					for(int k = 0;k < G2[v].size();k++){
						edge e = G2[v][k];
						if(d[e.to] > d[v] + e.cost){
							d[e.to] = d[v] + e.cost;
							que.push(P(d[e.to],e.to));
						}
					}
				}
				mi = min(mi,d[n - 1]);
			}
		}
		cout << mi << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 3164
final_rank: 16

code_snippet: #include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<functional>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<numeric>
#include<limits>
#include<iterator>

#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define rep(i,n) for(int i=0; i<n; i++)
#define INF (1<<20)

using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ll, char> plc;

int main()
{
	int n,m;
	int d[110][110], M[110][110];
	while (cin >> n >> m, n) 
	{
		rep(i, n)rep(j, n) { d[i][j] = INF; M[i][j] = 0; };
		rep(i, n)d[i][i] = 0;
		rep(i, m) {
			int a, b, c;
			cin >> a >> b >> c;
			a--, b--;
			d[a][b] = d[b][a] = c;
			M[a][b] = M[b][a] = 1;
		}

		rep(k, n)rep(i, n)rep(j, n)
			d[i][j] = min(d[i][j], d[i][k] + d[k][j]);

		int ans = d[0][n - 1];
		rep(k, n)rep(i, n)rep(j, n)
			if (M[i][k] && M[k][j])ans = min(ans, d[0][i] + d[j][n - 1]);

		cout << ans << endl;
	} 
	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3172
final_rank: 11

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

using namespace std;

#define INF 100000000

int main() {
	int n, m;
	while (cin >> n >> m) {
		if (n == 0 && m == 0) {
			break;
		}
		vector< vector<int> > cc(n, vector<int>(n, INF));
		vector< vector<int> > d(n, vector<int>(n, INF));
		int a, b, c;
		for (int i = 0; i < m; i++) {
			cin >> a >> b >> c;
			cc[a-1][b-1] = cc[b-1][a-1] = d[a-1][b-1] = d[b-1][a-1] = c;
		}
		//cout << 'a' << endl;
		for (int k = 0; k < n; k++) {
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < n; j++) {
					cc[i][j] = min(cc[i][j], cc[i][k]+cc[k][j]);
				}
			}
		}

		for (int i = 0; i < n; i++) {
			cc[i][i] = 0;
		}

		//cout << 'b' << endl;
		int MIN = cc[0][n-1];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				for (int k = 0; k < n; k++) {
					if (d[i][j] != INF && d[j][k] != INF) {
						MIN = min(MIN, cc[0][i]+cc[k][n-1]);
					}
				}
			}
		}
		cout << MIN << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 1304
final_rank: 7

code_snippet: //1.includes
#include<iostream>
#include<algorithm>
#include<functional>
#include<queue>
#include<vector>
#include<tuple>
using namespace std;
//2.defines
#define INF 1000000000000000000LL//(10^18)
#define MAX_N 300000
#define MAX_C 3
#define BLACK 2
#define GRAY 1
#define WHITE 0
//3.variables
priority_queue<tuple<long long, long long, long long>, vector<tuple<long long, long long, long long> >, greater<tuple<long long, long long, long long> > >Q;
vector<pair<long long, long long> >X[MAX_N];
long long dist[MAX_N][MAX_C];
long long color[MAX_N][MAX_C];
long long N, M, A, B, C;
//4.functions
void _memset() {
	for (int i = 0; i < MAX_N; i++) {
		for (int j = 0; j < MAX_C; j++) {
			dist[i][j] = 0;
			color[i][j] = 0;
		}
	}
	for (int i = 0; i < MAX_N; i++) {
		X[i].clear();
	}
	while (!Q.empty()) {
		Q.pop();
	}
}
long long dijkstra(long long s, long long t) {
	for (int i = 0; i < MAX_N; i++) {
		for (int j = 0; j < MAX_C; j++) {
			dist[i][j] = INF;
		}
	}
	color[s][0] = GRAY;
	Q.push(make_tuple(0, 0, s));//Q.first=dist,second=time,third=place
	dist[s][0] = 0;
	while (!Q.empty()) {
		tuple<long long, long long, long long>tup = Q.top();
		Q.pop();
		long long a1 = get<0>(tup);
		long long a2 = get<1>(tup);
		long long a3 = get<2>(tup);
		for (int i = 0; i < X[a3].size(); i++) {
			long long a4 = X[a3][i].first;
			long long a5 = X[a3][i].second;
			if (a2 == 0) {
				if (dist[a4][0] > a1 + a5) {
					dist[a4][0] = a1 + a5;
					Q.push(make_tuple(dist[a4][0], 0, a4));
				}
				if (dist[a4][1] > a1) {
					dist[a4][1] = a1;
					Q.push(make_tuple(dist[a4][1], 1, a4));
				}
			}
			if (a2 == 1) {
				if (dist[a4][2] > a1) {
					dist[a4][2] = a1;
					Q.push(make_tuple(dist[a4][2], 2, a4));
				}
			}
			if (a2 == 2) {
				if (dist[a4][2] > a1 + a5) {
					dist[a4][2] = a1 + a5;
					Q.push(make_tuple(dist[a4][2], 2, a4));
				}
			}
		}
	}
	long long minx = INF;
	minx = min(minx, dist[t][0]);
	minx = min(minx, dist[t][2]);
	return minx;
}
//5.main
int main() {
	while (true) {
		_memset();
		cin >> N >> M;
		if (N == 0 && M == 0) { break; }
		for (int i = 1; i <= M; i++) {
			cin >> A >> B >> C;
			X[A].push_back(make_pair(B, C));
			X[B].push_back(make_pair(A, C));
		}
		long long dist2 = dijkstra(1, N);
		if (dist2 >= INF) { dist2 = -1; }
		cout << dist2 << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 24152
final_rank: 19

code_snippet: #include <iostream>
using namespace std;

int main() {
	int i,j,k,a,b,c,m,n,an,g[100][100],lk[100][100],ln[100];
	while(cin >> n >> m && n>0) {
	for (i=0;i<100;i++) { ln[i]=0; for (j=0;j<100;j++) { g[i][j]=99999999; if (i==j) g[i][j]=0;}}	
	for (i=0;i<m;i++) {
		cin >> a >> b >> c; a--; b--;
		g[a][b]=g[b][a]=c; lk[a][ln[a]++]=b; lk[b][ln[b]++]=a;
	}
	for (k=0;k<n;k++) for (i=0;i<n;i++) for (j=0;j<n;j++) {c=g[i][k]+g[k][j]; if (g[i][j]>c) g[i][j]=c;}
	an=99999999;
	for (k=0;k<n;k++) for (i=0;i<ln[k];i++) for (j=0;j<ln[k];j++) {
	c=g[0][lk[k][i]]+g[lk[k][j]][n-1]; if (i!=j && c<an) an=c;}
	if (an>g[0][n-1]) an=g[0][n-1];
	if (n==2) an=g[0][1]; 
	cout << an << endl;
	} 
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 1236
final_rank: 2

code_snippet: #include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,mm;
int a[302],b[302],c[302],mn[302];
int ans;
int slv();

int main(){
  while(1){
    cin>>n>>m;
    if(n==0 && m==0)break;
    mm=m;
    ans=2000000;
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    memset(c,0,sizeof(c));
    for(int i=2;i<=n;i++)mn[i]=2000000;
    for(int i=1;i<=m;i++)cin>>a[i]>>b[i]>>c[i];

    mn[1]=0;
    slv();
    mm++;
    for(int i=1;i<=n;i++){
      for(int j=1;j<=m;j++){
	if(a[j]==i){
	  for(int k=1;k<=m;k++){
	    if(a[k]==b[j]){
	      a[mm]=i;
	      b[mm]=b[k];
	      for(int ii=2;ii<=n;ii++)mn[ii]=2000000;
	      mn[1]=0;
	      slv();
	    }
	    else if(b[k]==b[j]){
	      a[mm]=i;
	      b[mm]=a[k];
	      for(int ii=2;ii<=n;ii++)mn[ii]=2000000;
	      mn[1]=0;
	      slv();
	    }
	  }
	}
	else if(b[j]==i){
	  for(int k=1;k<=m;k++){
	    if(a[k]==a[j]){
	      a[mm]=i;
	      b[mm]=b[k];
	      for(int ii=2;ii<=n;ii++)mn[ii]=2000000;
	      mn[1]=0;
	      slv();
	    }
	    else if(b[k]==a[j]){
	      a[mm]=i;
	      b[mm]=a[k];
	      for(int ii=2;ii<=n;ii++)mn[ii]=2000000;
	      mn[1]=0;
	      slv();
	    }
	  }
	}
      }
    }
    
    cout<<ans<<endl;
  }
}

int slv(){
  for(int cnt=0;cnt<2;cnt++){
    for(int i=1;i<=n;i++){
      for(int j=1;j<=mm;j++){
	if(a[j]==i){
	  mn[b[j]]=min(mn[b[j]],mn[a[j]]+c[j]);
	  mn[a[j]]=min(mn[a[j]],mn[b[j]]+c[j]);
	}
	else if(b[j]==i){
	  mn[a[j]]=min(mn[a[j]],mn[b[j]]+c[j]);
	  mn[b[j]]=min(mn[b[j]],mn[a[j]]+c[j]);
	}
      }
    }
  }
  ans=min(ans,mn[n]);
}
accuracy: 0.3333333333333333
time_ms: 120
memory_kb: 1168
final_rank: 20

code_snippet: #include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i,n) for (int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define ALL(a) (a).begin(),(a).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
const int INF=1e9;
const int MOD=100000;
int B=1000000;
struct edge{int to,cost;};
int N,M;
int d[100][100];
vector<edge>G[100];
void dijkstra(int s){
      d[s][s]=0;
      priority_queue<P,vector<P>,greater<P> >Q;
      Q.push(P(0,s));
      while(!Q.empty()){
            P p=Q.top();Q.pop();
            int v=p.second;
            if(p.first>d[s][v])continue;
            for(int i=0;i<G[v].size();i++){
                  edge e=G[v][i];
                  if(d[s][e.to]>d[s][v]+e.cost){
                        d[s][e.to]=d[s][v]+e.cost;
                        Q.push(P(d[s][e.to],e.to));
                  }
            }
      }
}
int main(){
      while(cin>>N>>M&&(N||M)){
            REP(i,N)G[i].clear();
            REP(i,100)REP(j,100)d[i][j]=INF;
//            REP(i,N)REP(j,N)printf("%d%c",d[i][j],j==N-1?'\n':' ');
            REP(i,M){
                  int a,b,c;
                  cin>>a>>b>>c;a--;b--;
                  G[a].PB((edge){b,c});
                  G[b].PB((edge){a,c});
            }
            REP(i,N)dijkstra(i);
            vector<P>V;
            set<P> f;
            REP(i,N){
                  REP(j,G[i].size()){
                        int mid=G[i][j].to;
                        REP(k,G[mid].size()){
                              if(f.count(P(i,G[mid][k].to))==0){
                                    f.insert(P(i,G[mid][k].to));
                                    V.PB(P(i,G[mid][k].to));
                              }
                        }
                  }
            }
            int ans=INF;
            REP(i,V.size()){
                  if(ans>d[0][V[i].first]+d[V[i].second][N-1]){
                        ans=d[0][V[i].first]+d[V[i].second][N-1];
//                        cout<<V[i].first<<" "<<V[i].second<<" "<<ans<<endl;
                  }
            }
            cout<<ans<<endl;
      }
}
accuracy: 1
time_ms: 30
memory_kb: 1356
final_rank: 3

code_snippet: #include <iostream>
using namespace std;
#define rep(x,to) for(int x=0; x<(to); ++(x))
#define INF 1<<29
int main() {
    int m,n;

    while(cin >> n >> m, m) {
        int d[100][100];
        bool ok[100][100] = {};
        rep(i,100) {
            fill(d[i],d[i]+100,INF);
        }
        rep(i,100) {
            d[i][i] = 0;
        }
        int s,t,cost;
        rep(i,m) {
            cin >> s >> t >> cost;
            s--, t--;
            d[s][t] = d[t][s] = cost;
            ok[s][t] = ok[t][s] = true;
        }
        rep(i,n) {
            rep(j,n) {
                rep(k,n) {
                    d[j][k] = min(d[j][k], d[j][i] + d[i][k]);
                }
            }
        }
        int ans = d[0][n-1];
        rep(i,n) {
            rep(j,n) {
                if(i==j) continue;
                rep(k,n) {
                    if(k==i || k==j) continue;
                    if(ok[i][k] && ok[k][j]) {
                        ans = min(ans, d[0][i] + d[j][n-1]);
                        break;
                    }
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 1216
final_rank: 9

code_snippet: #include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iterator>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
#include <fstream>
#include <iomanip>
//#include <utility>
//#include <memory>
//#include <functional>
//#include <deque>
//#include <cctype>
//#include <ctime>
//#include <numeric>
//#include <list>
//#include <iomanip>

//#if __cplusplus >= 201103L
//#include <array>
//#include <tuple>
//#include <initializer_list>
//#include <forward_list>
//
//#define cauto const auto&
//#else

//#endif

using namespace std;


typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()
#define DEBUG
#ifdef DEBUG
#define dump(x)  cout << #x << " = " << (x) << endl;
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#else
#define dump(x) 
#define debug(x) 
#endif
#define LDcout(x,n) fixed<<setprecision(n)<<x

#define MOD 1000000007LL
#define EPS 1e-8
static const int INF=INT_MAX/2;

void mainmain(){
	int n,m;
	while(cin>>n>>m,n){
		vvint wf1;
		vvint wf2;
		initvv(wf1,n,n,0);
		initvv(wf2,n,n,INF);
		rep(i,n) wf1[i][i]=wf2[i][i]=0;
		rep(i,m){
			int a,b,c;
			cin>>a>>b>>c;
			a--; b--;
			wf1[a][b]=wf1[b][a]=1;
			wf2[a][b]=wf2[b][a]=c;
		}
		rep(k,n) rep(i,n) rep(j,n){
			wf2[i][j]=min(wf2[i][j],wf2[i][k]+wf2[k][j]);
		}
		int ans=wf2[0][n-1];
		rep(i,n){
			rep(j,n){
				rep(k,n){
					if(wf1[i][k]&&wf1[k][j]){
						ans=min(ans,wf2[0][i]+wf2[j][n-1]);
					}
				}
			}
		}
		cout<<ans<<endl;
	}
}


signed main() {
	ios_base::sync_with_stdio(false);
  	cout<<fixed<<setprecision(0);
    mainmain();
}
accuracy: 1
time_ms: 50
memory_kb: 1308
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;

const int INF = 1e+9;
typedef pair<int, int> P;

struct node {
	int c, to, t;
	node(int c, int to, int t) : c(c), to(to), t(t){}
};

bool operator > (const node& n1, const node& n2){
	return (n1.c == n2.c) ? n1.to > n2.to : n1.c > n2.c;  
}

int N, M;
vector<P> G[101];
int d[101][2];

void dijkstra(int s){
	for(int i = 0; i <= N; ++i){
		d[i][0] = INF;
		d[i][1] = INF;
	}
	priority_queue<node, vector<node>, greater<node>> que;
	que.push(node(0, s, 1));
	d[s][1] = 0;
	while(!que.empty()){
		node n = que.top();
		que.pop();
		if(n.c > d[n.to][n.t])
			continue;
		for(auto& u : G[n.to]){
			if(d[u.first][n.t] > d[n.to][n.t] + u.second){
				d[u.first][n.t] = d[n.to][n.t] + u.second;
				que.push(node(d[u.first][n.t], u.first, n.t));
			}
			if(n.t){
				if(G[u.first].empty())
					continue;
				for(auto w : G[u.first]){
					d[w.first][0] = min(d[w.first][0], d[n.to][1]);
					que.push(node(d[w.first][0], w.first, 0));
				}
			}
		}
	}
}

int main() {
	while(cin >> N >> M && (N || M)){
		for(int i = 0; i <= N; ++i)
			G[i].clear();
		int a, b, c;
		for(int i = 0; i < M; ++i){
			scanf("%d%d%d", &a, &b, &c);
			G[a].push_back(P(b, c));
			G[b].push_back(P(a, c));
		}
		dijkstra(1);
		cout << d[N][0] << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1264
final_rank: 1