Test instructions: from 1 to N and from N to 1 without repeating the edge, (if the point is the travel quotient problem), ask the shortest
Ideas:
Minimum cost maximum flow
Build a super-source it has a capacity of 2 and a cost of 0 to the first point
Then the capacity of each connected point is 1
Just find the two least expensive way
Then we can.
Code:
#include <iostream> using namespace std;
#include <stdio.h> #include <cstring> #include <algorithm> #include <queue> const int MAXN = 200;
const int MAXM = 200 *200;
const int INF = 0X3F3F3F3F;
struct node {int v,ne,cap,flow,cost;
void get (int a,int b,int c,int d) {v = A;
cap = b;
Cost = C;
ne = D;
flow = 0;
}}E[MAXM];
int HEAD[MAXN];
int PRE[MAXN],DIS[MAXN];
int VIS[MAXN];
int n,tol;
int nn,mm;
void Addedge (int u,int v,int cap,int cost) {E[tol].get (v,cap,cost,head[u]);
Head[u] = tol++;
E[tol].get (U,0,-cost,head[v]);
HEAD[V] = tol++;
} bool SPFA (int s,int t) {queue<int> q;
for (int i = 0; I <= N; i++) {dis[i] = INF;
Vis[i] = 0;
Pre[i] =-1;
} Dis[s] = 0;
Vis[s] = 1;
Q.push (s);
while (!q.empty ()) {int u = q.front ();
Q.pop ();
Vis[u] = 0; for (int i = head[u]; I! =-1; i = e[i].ne) {//Start point of UNumber int v = E[I].V; if (E[i].cap > E[i].flow && dis[v] > Dis[u] + e[i].cost) {//Find a minimum cost of road dis[v] = Dis[u] + e[i]
. Cost; PRE[V] = i;
The record is the number of the forward pointing Edge (!vis[v]) {vis[v] = 1;
Q.push (v);
}}}} if (pre[t] = = 1)//return FALSE if it does not exist;
return true;
} int mincostmaxflow (int s,int t,int &cost) {int flow = 0;
Cost = 0;
while (SPFA (s,t)) {int Min = INF;
for (int i = pre[t];i! =-1; i = pre[e[i^1].v]) {//Go back//cout << i <<endl;
if (min > E[i].cap-e[i].flow) Min = E[i].cap-e[i].flow;
}//find the smallest traffic in the entire road for (int i = pre[t]; i = 1; i = pre[e[i^1].v]) {e[i].flow + = min;
E[i^1].flow-=min;//e[i^1] Represents the I reverse side cost + = E[i].cost * Min;
} flow + = Min;
} Return flow;//If there are two lines then flow = = 2} void solve () {int ans = 0;
int t;
Addedge (NN + 1, 1, 2, 0);//Build source S =nn + 1 t = mincostmaxflow (nn + 1,nn, ans);
if (t = = 2) printf ("%d\n", ans);
else printf ("Back to jail\n");
} int main () {while (scanf ("%d%d", &nn,&mm)!=eof) {if (NN = = 0) break; N = NN + 1;
Including the starting point tol = 0;
memset (head,-1,sizeof (head));
int a,b,c;
for (int i = 1; I <= MM; i++) {scanf ("%d%d%d", &a,&b,&c);
Addedge (A,B,1,C);
Addedge (B,A,1,C);
Solve ();
} solve ();
for (int i = 0; I <tol; i++)//cout << e[i ^ 1].v <<endl;
} return 0; }