Double Shortest Paths CSU
来源:互联网 发布:怎么做淘客软件 编辑:程序博客网 时间:2024/06/05 03:55
题目:
题目地址
题目大意:
有两个人要从点1走到点n;给出路径之间第一个人走所需要的费用和第二个人走所需要的费用(第二个人如果走的路是第一个人走过的,那么就在第一个人所需的 费用上再加上第二次的费用)
求两个人一共所需要的最小费用!
思路:
比赛的时候翻书看到费用流就感觉很像,可惜从来没学过也没写过费用流,照着模板敲也因为不理解费用流(qaq所以有几个地方需要更改也不知道)根本跑不出结果。
别人的解释:
两点之间建两条边,分别为 容量1费用d 和 容量1费用d+a。由于模板求的是最小费用最大流,而本题需要限制流量为2,所以需要另设源点和汇点(容量为2) 来限制流量.
代码:
(这是用spfa增广的算法,先当模板凑合用,稍微理解一点点用法了)
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<string>#include<vector>#include<stack>#include<bitset>#include<cstdlib>#include<cmath>#include<set>#include<list>#include<deque>#include<map>#include<queue>using namespace std;typedef long long ll;const double PI = acos(-1.0);const double eps = 1e-6;const int INF = 0x3f3f3f3f;const int maxn = 6789;int T,n,m;struct Edge{ int to,next,cap,flow,cost; Edge(){} Edge(int _to,int _next,int _cap,int _flow,int _cost) { to=_to;next=_next;cap=_cap;flow=_flow;cost=_cost; }}e[maxn];int tot;int head[maxn];//邻接表存图专用int pre[maxn]; //储存前驱顶点int dis[maxn]; //储存到源点s的距离bool vis[maxn];//是否被放进队列中,spfa专用int N;//节点总个数,节点编号从0~N-1void init(int n){ tot=0; N=n+2; memset(head,-1,sizeof(head));}void add_edge(int u,int v,int cap,int cost){ e[tot]=Edge(v,head[u],cap,0,cost); head[u]=tot++; e[tot]=Edge(u,head[v],0,0,-cost); head[v]=tot++;}bool spfa(int s,int t)//spfa增广{ queue<int>q; for (int i=0;i<N;i++) { dis[i]=INF; vis[i]=false; pre[i]=-1; } dis[s]=0; vis[s]=true; q.push(s); while(!q.empty()) { int u=q.front(); q.pop(); vis[u]=false; for(int i=head[u];i!=-1;i=e[i].next) { int v=e[i].to; if(e[i].cap>e[i].flow&&dis[v]>dis[u]+e[i].cost) { dis[v]=dis[u]+e[i].cost; pre[v]=i; if(!vis[v]) { vis[v] = true; q.push(v); } } } } if(pre[t]==-1) return false; else return true;}//返回的是最大流,cost存的是最小费用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].to]) { if(Min>e[i].cap-e[i].flow) Min=e[i].cap-e[i].flow; } for(int i=pre[t];i!=-1;i=pre[e[i^1].to]) { e[i].flow+=Min; e[i^1].flow-=Min; cost+=e[i].cost*Min; } flow+=Min; } return flow;}int main(){ int n,m; int no=0; while(scanf("%d%d",&n,&m)!=EOF) { init(n); int s=0,t=n+1; //建立一个超源点和超汇点0,n+1 //因为0到1和n到n+1这两条边肯定会走两次,于是cap设为2,费用设为0 add_edge(s,1,2,0); add_edge(n,t,2,0); for(int i=1;i<=m;i++) { int u,v,a,b; scanf("%d%d%d%d",&u,&v,&a,&b); add_edge(u,v,1,a);//因为一条边只能一个人走,于是cap(容量)置为1 add_edge(u,v,1,a+b); } int ans=0; minCostMaxflow(s,t,ans); printf("Case %d: %d\n",++no,ans); } return 0;}
阅读全文
0 0
- Double Shortest Paths CSU
- 【费用流】CSU 1506 Double Shortest Paths
- CSU 1506: Double Shortest Paths(最小费用最大流)
- CSU 1506 - Double Shortest Paths(网络流’最小费用流)
- CSU 1506 Double Shortest Paths(最小费用流)
- CSU 1506 Double Shortest Paths(最小费用最大流)
- CSU 1506 Double Shortest Paths【最小费用最大流】
- CSU 1506: Double Shortest Paths(最小费用流)(湖南省第十届省赛)
- CSU 1506 Problem D: Double Shortest Paths(最小费用最大流)
- uva 12821 Double Shortest Paths
- Double Shortest Paths 网络流
- 费用流 csu1506 Double Shortest Paths
- 湖南第十届省赛 Double Shortest Paths
- CSU1506 Double Shortest Paths 最小费用最大流入门题
- Double Shortest Paths(费用流)2014年省赛D题
- (Shortest Paths Algorithm)
- csu Problem C: Double
- Shortest paths and matrix multiplication
- java webservice
- Oracle触发器用法实例详解
- SpringMVC
- 1062. Talent and Virtue (25)
- POJ
- Double Shortest Paths CSU
- Android第五天之ImageView图像视图
- word-break-ii
- 2017ICPCECIC MJF wants to work
- Spring思维导图,让Spring不再难懂(cache篇)
- centos Nginx Django uwsgi踩坑之ModuleNotFoundError: No module named 'django'
- 求m和n的算法题
- 洛谷P1848 [USACO12OPEN]书架Bookshelf
- Spring Bean的生命周期