UVa 10806 & cost stream + stream of consciousness ...

Source: Internet
Author: User

Test instructions

An no-go graph, which asks for two paths from S to t that are not duplicated.

SOL:

Network Flow why Dick? Because of the capacity of the network flow, traffic, the cost can be a lot of problems to the corresponding conversion, and then it is very dick.

For this problem, is not to not repeat it? is not a side can only walk once? Then the upper bound of the capacity is 1. Is there a two? Then the total traffic is 2. Not with the right? So add a fee.

  

WA was miserable, and finally found that the side from 0 began to remember the difference or later will change some very strange side ... It's silk.

Code

  

/*==========================================================================# last modified:2016-03-07 14:07# filename:uva10806.cpp# Description: ==========================================================================*/ #define ME Acrossthesky #include <cstdio> #include <cmath> #include <ctime> #include <string> #in Clude <cstring> #include <cstdlib> #include <iostream> #include <algorithm> #include <set&gt ; #include <map> #include <stack> #include <queue> #include <vector> #define LOWBIT (x) (x) & (-X ) #define for (I,A,B) for ((i) = (a);(i) <= (b);(i) + +) #define FORP (I,A,B) for (int i= (a); i<= (b); i++) #define FORM (i,a,b ) for (int i= (a); i>= (b); i--) #define LS (A, a) (((a) + (b)) << 1) #define RS (A, B) (((a) + (c)) >> 1) #define GETLC (a) ch[(a)][0] #define GETRC (a) ch[(a)][1] #define MAXN #define MAXM 100000 #define PI 3.1415926535898 #define _E 2. 718281828459 #define INF 1070000000 using NAMESPAce STD; typedef long Long LL;  typedef unsigned long long ull;     Template<class t> inline void read (t& num) {bool start=false,neg=false;     char c;     num=0;         while ((C=getchar ())!=eof) {if (c== '-') start=neg=true;             else if (c>= ' 0 ' && c<= ' 9 ') {start=true;         num=num*10+c-' 0 ';     } else if (start) break; } if (neg) num=-num; }/*==================split line==================*/struct edge{int from,to,w,c;} E[maxm];int sume,flow,ans,n,m;int d[maxn],first[maxn],next[maxm],from[maxn];bool inq[maxn];void addedge (int x,int y, int cap,int cost) {sume++; e[sume].from=x; e[sume].to=y; e[sume].w=cap; e[sume].c=cost;next[sume]=first[x]; first[x]= sume;sume++; E[sume].from=y; E[sume].to=x; e[sume].w=0; E[sume].c=-cost;next[sume]=first[y]; First[y]=sume;} BOOL SPFA () {queue<int> q; Forp (i,0,n) D[i]=inf;memset (inq,false,sizeof (INQ)); memset (From,0,sizeof (from)); Q.push (0); d[0]=0; Inq[0]=true;while (!q.empty ()) {int Now=q.fronT (); Q.pop (); inq[now]=false;for (int i=first[now];i!=-1;i=next[i]) if (E[I].W && d[now]+e[i].c<d[e[i].to]) {d[e[i].to]= D[now]+e[i].c;from[e[i].to]=i;//q.push (e[i].to); if (!inq[e[i].to]) {Inq[e[i].to]=true;q.push (e[i].to);}}} if (D[n]==inf) return False;return true;} void Mincost () {int X=inf,i=from[n];while (i) {x=min (e[i].w,x); i=from[e[i].from];} Flow+=x;i=from[n];while (i) {e[i].w-=x;e[i^1].w+=x;//ans+= (X*E[I].C); i=from[e[i].from];} Ans+=d[n]*x;} void Reset () {sume=1; flow=0; Ans=0;memset (E,0,sizeof (e)); Forp (I,0,MAXN) First[i]=-1;memset (next,0,sizeof (next));} int main () {while (scanf ("%d", &n)!=eof) {reset (); if (n==0) return 0; Read (m); Forp (i,1,m) {int x,y,w; read (x); Read (y); Read (w); Addedge (x,y,1,w); Addedge (y,x,1,w);} addedge (0,1,2,0); Addedge (n,n+1,2,0); n++; while (SPFA ()) mincost (); if (flow<2) printf ("Back to jail\n"); else printf ("%d\n", ans);}}

UVa 10806 & cost stream + stream of consciousness ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.