题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=569&problem=4277&mosmsg=Submission+received+with+ID+2182664
题目大意:给定一个带权有向图(v,e),求两条互不相交的从1到v的路径(不经过同一个点),使两条路径的权和最小.
分析:即求从1到v的最小费用流,到v的流量为2,且每个点只能经过一次.加一个从v出发的点v',费用为0,容量为2,求1到v'的最小费用流即满足前一条件;将2到v-1每个点拆分成两个点,费用为0,容量为1,再求从1到v'的最小费用流即可.
一定要注意求mcmf时设置的INF要恰当!!!不然就溢出然后疯狂WA了!!!
1 #include2 #include 3 #include 4 #include 5 #include 6 using namespace std; 7 const int maxn=2005,maxm=20005,INF=1000000; 8 struct Edge{ 9 int from,to,cap,flow,cost;10 Edge(int u,int v,int c,int f,int w):from(u),to(v),cap(c),flow(f),cost(w){}11 };12 13 struct MCMF{14 int n,m;15 vector edges;16 vector G[maxn];17 int inq[maxn],d[maxn],p[maxn],a[maxn];18 19 void init(int n){20 this->n=n;21 this->m=0;22 for(int i=0;i m++);30 G[to].push_back(this->m++);31 }32 33 bool BellmanFord(int s,int t,int &flow,long long &cost){34 for(int i=0;i