題目鏈接:點擊查看
題目大意:給出一張 n 個點和 m 條邊組成的有向圖,現在問讓最短路變長的最小花費是多少
題目分析:增加最短路的最小花費,我們可以將最短路上的邊單獨拿出來,再求一下最小割就好了,用了封裝后的算法看起來非常舒服,但時間復雜度有點高。。看別人都是100ms左右跑完,我的代碼是950ms劃過
代碼:
?
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
using namespace std;typedef long long LL;typedef unsigned long long ull;const LL inf=0x3f3f3f3f3f3f3f3f;const int N=1e4+100;template<typename T>
struct Dinic
{const static int N=1e4+100;const static int M=2e4+100;const T inf=0x3f3f3f3f3f3f3f3f;struct Edge{int to,next;T w;}edge[M];//邊數int head[N],cnt;void addedge(int u,int v,T w){edge[cnt].to=v;edge[cnt].w=w;edge[cnt].next=head[u];head[u]=cnt++;edge[cnt].to=u;edge[cnt].w=0;//反向邊邊權設置為0edge[cnt].next=head[v];head[v]=cnt++;}int d[N],now[N];//深度 當前弧優化bool bfs(int s,int t)//尋找增廣路{memset(d,0,sizeof(d));queue<int>q;q.push(s);now[s]=head[s];d[s]=1;while(!q.empty()){int u=q.front();q.pop();for(int i=head[u];i!=-1;i=edge[i].next){int v=edge[i].to;T w=edge[i].w;if(d[v])continue;if(!w)continue;d[v]=d[u]+1;now[v]=head[v];q.push(v);if(v==t)return true;}}return false;}T dinic(int x,int t,T flow)//更新答案{if(x==t)return flow;T rest=flow,i;for(i=now[x];i!=-1&&rest;i=edge[i].next){int v=edge[i].to;T w=edge[i].w;if(w&&d[v]==d[x]+1){T k=dinic(v,t,min(rest,w));if(!k)d[v]=0;edge[i].w-=k;edge[i^1].w+=k;rest-=k;}}now[x]=i;return flow-rest;}void init(){memset(now,0,sizeof(now));memset(head,-1,sizeof(head));cnt=0;}T solve(int st,int ed){T ans=0,flow;while(bfs(st,ed))while(flow=dinic(st,ed,inf))ans+=flow;return ans;}
};template<typename T>
struct Dij
{const static int N=1e4+100;const static int M=1e4+100;struct Edge{int to,next;T w;}edge[M];int head[N],cnt;//鏈式前向星 T d[N];bool vis[N];void addedge(int u,int v,T w){edge[cnt].to=v;edge[cnt].w=w;edge[cnt].next=head[u];head[u]=cnt++;}struct Node{int to;T w;Node(int TO,T W){to=TO;w=W;}bool operator<(const Node& a)const{return w>a.w;}};void Dijkstra(int st){priority_queue<Node>q;memset(vis,false,sizeof(vis));memset(d,0x3f,sizeof(d));d[st]=0;q.push(Node(st,0));while(q.size()){Node cur=q.top();int u=cur.to;q.pop();if(vis[u])continue;vis[u]=true;for(int i=head[u];i!=-1;i=edge[i].next)//掃描出所有邊 {int v=edge[i].to;T w=edge[i].w;if(d[v]>d[u]+w)//更新 {d[v]=d[u]+w;q.push(Node(v,d[v]));}}}}void init(){memset(head,-1,sizeof(head));cnt=0; }
};Dinic<LL>t;Dij<LL>d1,d2;int main()
{
#ifndef ONLINE_JUDGE
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
#endif
// ios::sync_with_stdio(false);int w;cin>>w;while(w--){t.init(),d1.init(),d2.init();int n,m,st=N-1,ed=st-1;scanf("%d%d",&n,&m);while(m--){int u,v,w;scanf("%d%d%d",&u,&v,&w);d1.addedge(u,v,w);d2.addedge(v,u,w);}d1.Dijkstra(1);d2.Dijkstra(n);if(d1.d[n]==inf){puts("0");continue;}for(int i=1;i<=n;i++)for(int j=d1.head[i];j!=-1;j=d1.edge[j].next){int u=i,v=d1.edge[j].to;LL w=d1.edge[j].w;if(d1.d[u]+w+d2.d[v]==d1.d[n])t.addedge(u,v,w);}t.addedge(st,1,inf);t.addedge(n,ed,inf);printf("%lld\n",t.solve(st,ed));}return 0;
}
?
總結
以上是生活随笔為你收集整理的HDU - 6582 Path(最短路+最大流)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。