#include<cstdio>
#include<cstring>
#include<queue>
#define INF 0x3f3f3f3f
using namespace std;
int g[160][160],vis[160],d[160],n,q,pre[160],res;
int spfa(int s,int t){
queue<int> q;
memset(vis,0,sizeof(vis));
memset(d,0x3f,sizeof(d));
d[s]=0;
vis[s]=1;
q.push(s);
while(!q.empty()){
int u=q.front();q.pop();
vis[u]=0;
for(int i=1;i<=n;i++){
if(d[i]>d[u]+g[u][i]){
d[i]=d[u]+g[u][i];
pre[i]=u;
if(!vis[i]){
vis[i]=1;
q.push(i);
}
}
}
}
if(d[t] == INF)
return 0;
for(int i=t;i!=s;i=pre[i]){
res+=g[pre[i]][i];
g[i][pre[i]]=-g[pre[i]][i];
g[pre[i]][i]=INF;
}
return 1;
}
int main(){
while(scanf("%d",&n) == 1 && n){
res=0;
memset(g,0x3f,sizeof(g));
scanf("%d",&q);
for(int i=0;i<q;i++){
int u,v,c;
scanf("%d%d%d",&u,&v,&c);
g[u][v]=g[v][u]=c;
}
if(spfa(1,n) && spfa(1,n))
printf("%d\n",res);
else
printf("Back to jail\n");
}
}
uva 10806 从一个点到另外1个点的2条不重复边的路径
最新推荐文章于 2022-01-16 13:08:03 发布