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.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以 ...
随机推荐
- 我想要革命想要解脱——bootstrap常见问题及解决方式
最近一个月,恍若隔世,天天加班,昨晚终于发版了,今天才喘一口气.有时候,即便你工作效率再怎么高,撸码再怎么快也无可避免的会加班.不信的话,可以先给你定一个交付时间,然后不断的给你加需求,就让你一个人做 ...
- iOS----------禁止输入汉字
说明: ^.*[\u4e00-\u9fa5].*$ 是否包含中文^[\u4E00-\u9FA5]+$ 是否全中文 - (BOOL)textField:(UITextField *)textField ...
- wap2app(五)-- 微信授权登录以及踩过的坑
应用场景是:用Hbuilder打包app,在app中点击微信授权登录或者某一操作,调起微信授权登录,用户授权后拿到用户信息. 一.登录插件配置 先配置微信登录参数 appid和appsecret,在m ...
- 使用 Dashboard - 每天5分钟玩转 Docker 容器技术(174)
上一节我们完成了 Kubernetes Dashboard 的安装,本节就来实践一下. Dashboard 界面结构 Dashboard 的界面很简洁,分为三个大的区域. 顶部操作区在这里用户可以搜索 ...
- Linux Logwatch的学习总结
Logwatch功能介绍 Logwatch是一款Perl脚本编写的.开源的日志分析工具.它能对原始的日志文件进行解析并转换成结构化格式的文档,也能根据您的使用情况和需求来定制报告.Logwatch的特 ...
- 爬虫之scrapy--基本操作
参考博客 :https://www.cnblogs.com/wupeiqi/p/6229292.html Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 其可以应用在数据挖掘, ...
- 【Python 25】52周存钱挑战5.0(datetime库和import)
1.案例描述 按照52周存钱法,存钱人必须在一年52周内,每周递存10元.例如,第一周存10元,第二周存20元,第三周存30元,直到第52周存520元. 记录52周后能存多少钱?即10+20+30+. ...
- 【Python 21】52周存钱挑战1.0
1.案例描述 按照52周存钱法,存钱人必须在一年52周内,每周递存10元.例如,第一周存10元,第二周存20元,第三周存30元,直到第52周存520元. 记录52周后能存多少钱?即10+20+30+. ...
- python Socket socketserver
Socket 套接字 socket的 类型 实现socket对象时传入 到socket 类中 socket.AF_INET 服务器间的通讯 IPv4 socket.AF_INET6 IPv6 sock ...
- [LeetCode] 10. 正则表达式匹配
题目链接:https://leetcode-cn.com/problems/regular-expression-matching/ 题目描述: 给定一个字符串 (s) 和一个字符模式 (p).实现支 ...