hdu-3416(最短路+网络流)
题意:给你一个有向权图,问你从S到E有几条最短路,每条边直走一次的情况下;
解题思路:每条边直走一次,最大流边权为1,因为要算几条最短路,那么能得到最短路的路径标记下,然后跑最大流
代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxm=100500;
const int maxn=1050;
struct Edge
{
int next;int to;int w;int fa;
}edge[maxm],edge2[maxm];
struct node
{
int num;
int dist;
node(int _num=0,int _dist=0):num(_num),dist(_dist){}
friend bool operator<(node a,node b)
{
return a.dist>b.dist;
}
};
int head[maxn],head2[maxn],cnt,cnt2;
int dist[maxn],n,m,Start,End;
int depth[maxn];
int x[maxm],y[maxm],w[maxm];
void add(int u,int v,int w)
{
edge[cnt].next=head[u];edge[cnt].to=v;edge[cnt].w=w;head[u]=cnt++;
}
void add2(int u,int v,int w)
{
edge2[cnt2].next=head2[u];edge2[cnt2].to=v;edge2[cnt2].w=w;edge2[cnt2].fa=u;head2[u]=cnt2++;
edge2[cnt2].next=head2[v];edge2[cnt2].to=u;edge2[cnt2].w=0;edge2[cnt2].fa=v;head2[v]=cnt2++;
}
void dij(int x)
{
memset(dist,inf,sizeof(dist));
priority_queue<node>q;q.push(node(x,0));dist[x]=0;
while(!q.empty())
{
node u=q.top();q.pop();int now=u.num;
for(int i=head[now];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
if(dist[v]>dist[now]+edge[i].w)
{
dist[v]=dist[now]+edge[i].w;q.push(node(v,dist[v]));
}
}
}
}
bool bfs()//分层;
{
memset(depth,0,sizeof(depth));
queue<int>q;
q.push(Start);
depth[Start]=1;
while(!q.empty())
{
int temp=q.front();
q.pop();
for(int i=head2[temp];i!=-1;i=edge2[i].next)
{
int v=edge2[i].to;
if(depth[v]||edge2[i].w<=0)
continue;
depth[v]=depth[temp]+1;
q.push(v);
}
}
return depth[End];//若为0表示没法到达也就是没有路径了;
}
int dfs(int u,int maxflow)
{
if(u==End)
return maxflow;
int add=0;
for(int i=head2[u];i!=-1&&add<maxflow;i=edge2[i].next)
{
int v=edge2[i].to;
if(depth[v]!=depth[u]+1)
continue;
if(edge2[i].w==0)
continue;
int tempflow=dfs(v,min(edge2[i].w,maxflow-add));
edge2[i].w-=tempflow;
edge2[i^1].w+=tempflow;
add+=tempflow;
}
return add;
}
int dinic()
{
int ans=0;
while(bfs())
{
ans+=dfs(Start,inf);
}
return ans;
}
int main()
{
int tt;
scanf("%d",&tt);
while(tt--)
{
scanf("%d%d",&n,&m);
memset(head,-1,sizeof(head));memset(head2,-1,sizeof(head2));cnt=cnt2=0;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&x[i],&y[i],&w[i]);add(x[i],y[i],w[i]);
}
scanf("%d%d",&Start,&End);
dij(Start);
for(int i=1;i<=m;i++)
if(dist[y[i]]==dist[x[i]]+w[i])
add2(x[i],y[i],1);
int ans=dinic();
printf("%d\n",ans);
}
}
hdu-3416(最短路+网络流)的更多相关文章
- HDU 5889 (最短路+网络流)
Barricade Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- HDU 3416 Marriage Match IV (最短路径,网络流,最大流)
HDU 3416 Marriage Match IV (最短路径,网络流,最大流) Description Do not sincere non-interference. Like that sho ...
- hdu 3416 Marriage Match IV (最短路+最大流)
hdu 3416 Marriage Match IV Description Do not sincere non-interference. Like that show, now starvae ...
- ACM: HDU 2544 最短路-Dijkstra算法
HDU 2544最短路 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descrip ...
- UESTC 30 &&HDU 2544最短路【Floyd求解裸题】
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- hdu 5521 最短路
Meeting Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- HDU 3605 Escape (网络流,最大流,位运算压缩)
HDU 3605 Escape (网络流,最大流,位运算压缩) Description 2012 If this is the end of the world how to do? I do not ...
- HDU 4289 Control (网络流,最大流)
HDU 4289 Control (网络流,最大流) Description You, the head of Department of Security, recently received a ...
- HDU 4292 Food (网络流,最大流)
HDU 4292 Food (网络流,最大流) Description You, a part-time dining service worker in your college's dining ...
- HDU - 2544最短路 (dijkstra算法)
HDU - 2544最短路 Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以 ...
随机推荐
- ejs常用语法
nodejs的模板引擎有很多, ejs是比较简单和容易上手的.常用的一些语法: 用<%...%>包含js代码 用<%=...%>输出变量 变量若包含 '<' '>' ...
- 广州.NET微软技术俱乐部 微信群有用信息集锦
考虑到广州.NET微软技术俱乐部 微信群 十分活跃. 有用信息很有可能被淹没. 所以建立此贴. 首先群的活跃是十分重要的. 所以我是不可能把群搞得像技术论坛和github一样, 因为微信群的定位我在& ...
- python 图片在线转字符画预览
文章链接:https://mp.weixin.qq.com/s/yiFOmljhyalE8ssAgwo6Jw 关于python图片转字符画,相信大家都不陌生,经常出现在 n个超有趣的python项目中 ...
- 仿9GAG制作过程(三)
有话要说: 这次准备讲述后台服务器的搭建以及前台访问到数据的过程. 成果: 准备: 安装了eclipse 安装了Tomcat7 安装了数据库管理工具:Navicat 搭建服务器: 用eclipse直接 ...
- macos 常用快捷键及操作
通用: 拷贝相当于window下的复制非苹果键盘(command == win option == alt control == ctrl)Command + C 拷贝(Copy)Command + ...
- 安装最新nodejs
# apt-get update # apt-get install -y python-software-properties software-properties-common # add-ap ...
- mysql的定时器
mysql定时器是系统给提供了event,而oracle里面的定时器是系统给提供的job.废话少说,下面创建表: create table mytable ( id int auto_incremen ...
- Oracle获取表字段名,字段类型,字段长度,注释
SELECT b.comments as 注释, a.column_name as 列名, a.data_type || '(' || a.data_length || ')' as 数据类型, a. ...
- 微信小程序发红包
背景: 近期一个朋友公司要做活动,活动放在小程序上.小程序开发倒是不难,不过要使用小程序给微信用户发红包,这个就有点麻烦 确定模式: 小程序目前没有发红包接口,要实现的话,只能是模拟红包,即小程序上做 ...
- Type '' cannot conform to protocol '' because it has requirements that cannot be satisfied
我有一个Objective-C协议,我试图在Swift类中实现.例如: @class AnObjcClass; @protocol ObjcProtocol <NSObject> - (v ...