博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU - 3416-Marriage Match IV (最大流 + 最短路)
阅读量:4552 次
发布时间:2019-06-08

本文共 3418 字,大约阅读时间需要 11 分钟。

 HDU - 3416:

参考:

题意:

   问一个带权值的图中,最多能跑几次最短路,每条路只能走一遍。

思路:

  这道题要利用最大流算法和最短路算法。先要跑两遍dji(),第一遍跑从起点S开始跑一遍正图,第二遍从终点T跑一遍反图。这样后枚举每一条边,看这条边的起点到S的距离+这条边的终点到T的距离+这条边长 == 最短路,如果成立,就把这条边 的容量设为1,加入网络流的图中。最后跑一下最大流就行了。

  自己在枚举点的时候没有搞清楚终点是T,wa了好几发。

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;//#pragma GCC optimize(3)//#pragma comment(linker, "/STACK:102400000,102400000") //c++#define lson (l , mid , rt << 1)#define rson (mid + 1 , r , rt << 1 | 1)#define debug(x) cerr << #x << " = " << x << "\n";#define pb push_back#define pq priority_queuetypedef long long ll;typedef unsigned long long ull;typedef pair
pll;typedef pair
pii;typedef pair
p3;//priority_queue
q;//这是一个大根堆q//priority_queue
,greater
>q;//这是一个小根堆q#define fi first#define se second//#define endl '\n'#define OKC ios::sync_with_stdio(false);cin.tie(0)#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行#define REP(i , j , k) for(int i = j ; i < k ; ++i)//priority_queue
, greater
>que;const ll mos = 0x7FFFFFFF; //2147483647const ll nmos = 0x80000000; //-2147483648const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18const int mod = 1e9+7;const double esp = 1e-8;const double PI=acos(-1.0);template
inline T read(T&x){ x=0;int f=0;char ch=getchar(); while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar(); while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x=f?-x:x;}/*-----------------------showtime----------------------*/ const int maxn = 2e5+9; int n,m,c,ans; int dis[2][maxn]; vector
g[2][maxn]; //dji void dji(int f,int soc){ for(int i=1; i<=n; i++)dis[f][i] = inf; priority_queue
que; dis[f][soc] = 0; que.push(pii(0,soc)); while(!que.empty()){ pii tmp = que.top();que.pop(); int u = tmp.se; if(dis[f][u] < -1*tmp.fi)continue; for(int i=0; i
dis[f][u] + g[f][u][i].se) { dis[f][v] = dis[f][u] + g[f][u][i].se; que.push(pii(-1*dis[f][v], v)); } } } } const int maxm = 1e6+9; struct Edge { int u,v,cap; Edge(){} Edge(int u,int v,int cap):u(u),v(v),cap(cap){} }es[maxm]; int R,S,T; vector
tab[maxn]; int diss[maxn],current[maxn]; void addedge(int u,int v,int cap){ tab[u].pb(R); es[R++] = Edge(u,v,cap); tab[v].pb(R); es[R++] = Edge(v,u,0); } bool BFS(){ queue
q;q.push(S); memset(diss,inf,sizeof(diss)); diss[S] = 0; while(!q.empty()){ int h = q.front();q.pop(); for(int i=0; i
0 &&diss[e.v] == inf){ diss[e.v] = diss[h] + 1; q.push(e.v); } } } return diss[T] < inf; } int DFS(int x,int maxflow){ if(x == T){ return maxflow; } for(int i=current[x]; i
0){ int flow = DFS(e.v, min(maxflow, e.cap)); if(flow>0){ e.cap -= flow; es[tab[x][i] ^ 1].cap += flow; return flow; } } } return 0; } int dinic(){ int ans = 0; while(BFS()){ int flow; memset(current,0,sizeof(current)); while(true){ flow = DFS(S,inf); if(flow > 0) ans += flow; else break; } } return ans; }int main(){ int t;scanf("%d" , &t); while(t--){ scanf("%d%d", &n, &m); for(int i=1; i<=n; i++)g[0][i].clear(),g[1][i].clear(),tab[i].clear(); for(int i=1; i<=m; i++){ int u,v,c; scanf("%d%d%d", &u, &v, &c); if(v == u)continue; g[0][u].pb(pii(v,c)); g[1][v].pb(pii(u,c)); } scanf("%d%d", &S, &T); dji(0,S),dji(1,T); // debug(dis[0][n]); R = 0; for(int i=1; i<=n; i++){ for(int j=0; j
HDU - 3416

 

转载于:https://www.cnblogs.com/ckxkexing/p/9592901.html

你可能感兴趣的文章
AFNetworking
查看>>
unity3d Start执行不同时问题
查看>>
session
查看>>
JS只能输入数字
查看>>
Laravel 数据库连接, 数据库名,配置文件修改
查看>>
屌丝接盘侠们,孩子可能不是你们亲生的!
查看>>
BZOJ 1854 【SCOI2010】 游戏
查看>>
JavaScript - 匿名函数和闭包
查看>>
负载均衡下的资源文件配置/多站点下的资源文件夹共享(Windows IIS)
查看>>
MySQL firstmatch strategy
查看>>
MS SQL server 2014 创建用户及权限
查看>>
office很抱歉遇到一些临时服务器问题
查看>>
禁止键盘上的刷新键F5等
查看>>
SAP中对于获取订单的状态
查看>>
oracle PL/SQL块
查看>>
sklearn.preprocessing.LabelBinarizer
查看>>
C teaching
查看>>
分隔指定内容,提取章节数
查看>>
this point
查看>>
leetcode 30 Substring with Concatenation of All Words
查看>>