链接:

https://vjudge.net/problem/HDU-3416

题意:

Do not sincere non-interference。

Like that show, now starvae also take part in a show, but it take place between city A and B. Starvae is in city A and girls are in city B. Every time starvae can get to city B and make a data with a girl he likes. But there are two problems with it, one is starvae must get to B within least time, it's said that he must take a shortest path. Other is no road can be taken more than once. While the city starvae passed away can been taken more than once.

So, under a good RP, starvae may have many chances to get to city B. But he don't know how many chances at most he can make a data with the girl he likes . Could you help starvae?

思路:

先找出最短路,然后将最短路的每条边加入网络流的图,边权为1,跑最大流即可.

(找最短路SPFAwa了好几次,改成Dij就过了..玄学)(SPFA写错了,真的丢人)

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL; const int MAXN = 1e3+10;
const int INF = 1e9; struct Edge
{
int from, to, cap;
};
struct Road
{
int from, to, len;
};
struct Dj
{
int to, dis;
bool operator < (const Dj &that) const
{
return this->dis > that.dis;
}
};
vector<int> G[MAXN];
vector<int> GR[MAXN];
vector<int> GR2[MAXN];
vector<Edge> edges;
vector<Road> roads;
vector<Road> roads2;
int Dis[MAXN], Vis[MAXN], Dis2[MAXN];
int n, m, s, t; void AddEdge(int from ,int to, int cap)
{
edges.push_back(Edge{from, to, cap});
edges.push_back(Edge{to, from, 0});
G[from].push_back(edges.size()-2);
G[to].push_back(edges.size()-1);
} void Dij()
{
memset(Vis, 0, sizeof(Vis));
memset(Dis, MINF, sizeof(Dis));
Dis[s] = 0;
priority_queue<Dj> que;
que.push(Dj{s, 0});
while (!que.empty())
{
Dj u = que.top();
que.pop();
if (Vis[u.to])
continue;
Vis[u.to] = 1;
for (int i = 0;i < GR[u.to].size();i++)
{
Road &r = roads[GR[u.to][i]];
if (Dis[r.to] > Dis[u.to]+r.len)
{
Dis[r.to] = Dis[u.to]+r.len;
que.push(Dj{r.to, Dis[r.to]});
}
}
}
} //void SPFA2()
//{
// memset(Vis, 0, sizeof(Vis));
// memset(Dis2, MINF, sizeof(Dis2));
// Dis2[t] = 0;
// Vis[t] = 1;
// queue<int> que;
// que.push(t);
// while (!que.empty())
// {
// int u = que.front();
// que.pop();
// for (int i = 0;i < GR2[u].size();i++)
// {
// Road &r = roads2[GR2[u][i]];
// if (Dis2[r.to] > Dis2[u]+r.len)
// {
// Dis2[r.to] = Dis2[u]+r.len;
// if (Vis[r.to] == 0)
// {
// que.push(r.to);
// Vis[r.to] = 1;
// }
// }
// }
// }
//} bool Bfs()
{
memset(Dis, -1, sizeof(Dis));
queue<int> que;
Dis[s] = 0;
que.push(s);
while (!que.empty())
{
int u = que.front();
que.pop();
for (int i = 0;i < G[u].size();i++)
{
Edge &e = edges[G[u][i]];
if (e.cap > 0 && Dis[e.to] == -1)
{
Dis[e.to] = Dis[u]+1;
que.push(e.to);
}
}
}
return Dis[t] != -1;
} int Dfs(int u, int flow)
{
if (u == t)
return flow;
int res = 0;
for (int i = 0;i < G[u].size();i++)
{
Edge &e = edges[G[u][i]];
if (e.cap > 0 && Dis[e.to] == Dis[u]+1)
{
int tmp = Dfs(e.to, min(e.cap, flow));
flow -= tmp;
e.cap -= tmp;
res += tmp;
edges[G[u][i]^1].cap += tmp;
if (flow == 0)
break;
}
}
if (res == 0)
Dis[u] = -1;
return res;
} int MaxFlow()
{
int res = 0;
while (Bfs())
res += Dfs(s, INF);
return res;
} int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin >> T;
while (T--)
{
cin >> n >> m;
for (int i = 1;i <= n;i++)
G[i].clear(), GR[i].clear();
edges.clear(), roads.clear();
int u, v, w;
for (int i = 1;i <= m;i++)
{
cin >> u >> v >> w;
roads.push_back(Road{u, v, w});
GR[u].push_back(roads.size()-1);
// roads2.push_back(Road{v, u, w});
// GR2[v].push_back(roads2.size()-1);
}
cin >> s >> t;
Dij();
// SPFA2();
if (Dis[t] == MINF)
{
cout << 0 << endl;
continue;
}
// cout << 1 << endl;
for (int i = 1;i <= n;i++)
{
for (int j = 0;j < GR[i].size();j++)
{
Road &r = roads[GR[i][j]];
// cout << Dis[r.from] << ' ' << Dis[r.to] << ' ' << r.len << endl;
if (Dis[r.from]+r.len == Dis[r.to])
{
// cout << r.from << ' ' << r.to << endl;
AddEdge(r.from, r.to, 1);
}
}
}
int res = MaxFlow();
cout << res << endl;
} return 0;
}

HDU-3416-MarriageMatch4(最大流,最短路)的更多相关文章

  1. HDU 3416 Marriage Match IV (最短路建图+最大流)

    (点击此处查看原题) 题目分析 题意:给出一个有n个结点,m条单向边的有向图,问从源点s到汇点t的不重合的最短路有多少条,所谓不重复,意思是任意两条最短路径都不共用一条边,而且任意两点之间的边只会用一 ...

  2. Mining Station on the Sea HDU - 2448(费用流 || 最短路 && hc)

    Mining Station on the Sea Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  3. hdu 3416 Marriage Match IV (最短路+最大流)

    hdu 3416 Marriage Match IV Description Do not sincere non-interference. Like that show, now starvae ...

  4. HDU 3416 Marriage Match IV (最短路径,网络流,最大流)

    HDU 3416 Marriage Match IV (最短路径,网络流,最大流) Description Do not sincere non-interference. Like that sho ...

  5. HDU - 3416-Marriage Match IV (最大流 + 最短路)

    HDU - 3416:http://acm.hdu.edu.cn/showproblem.php?pid=3416 参考:https://www.cnblogs.com/kuangbin/archiv ...

  6. java8学习之流的短路与并发流

    并发流: 从api的角度来看,其实跟咱们之前一直在用的stream()方式差不多,但是底层是有明显的不同,所以这里初步先对并发流有一个基本的认识, 说到串行与并行,最直观的感受就是效率的不同,所以下面 ...

  7. HDU 3416:Marriage Match IV(最短路+最大流)

    http://acm.hdu.edu.cn/showproblem.php?pid=3416 题意:给出n个点m条边,边信息分别是两个端点和一个费用,再给出一个起点和一个终点,问从起点到终点的完全不相 ...

  8. HDU 3416 Marriage Match IV (求最短路的条数,最大流)

    Marriage Match IV 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/Q Description Do not si ...

  9. O - Marriage Match IV - hdu 3416(最短路+最大流)

    题目大意:在城市A的男孩想去城市B的女孩,不过他去城市B必须走最短路,并且走过的路不可以再走,问他最多能看这个女孩多少次.   分析:因为这个男孩直走最短路,所以我们必须求出来所有最短路径上的路,怎么 ...

  10. HDU 3416 Marriage Match IV 【最短路】(记录路径)+【最大流】

    <题目链接> 题目大意: 给你一张图,问你其中没有边重合的最短路径有多少条. 解题分析: 建图的时候记得存一下链式后向边,方便寻找最短路径,然后用Dijkstra或者SPFA跑一遍最短路, ...

随机推荐

  1. sudo apt -y upgrade

    sudo apt -y upgrade     直接upgrade,不再询问y/n 但是如果是sudo apt-get install scilab -y 那么,就不再显示上图中的信息,即当安装包的时 ...

  2. Scratch少儿编程系列:(十一)Scratch编程之简单见解

    一.Scratch官网的说明     With Scratch, you can program your own interactive stories, games, and animations ...

  3. SpringCloud 和 Dubbo 有哪些区别?

    首先,他们都是分布式管理框架.    dubbo 是二进制传输,占用带宽会少一点.SpringCloud是http 传输,带宽会多一点,同时使用http协议一般会使用JSON报文,消耗会更大.    ...

  4. Python中调用c语言(简单版)

    Python中有时需要调用c程序中的函数.使用ctype库可以很方便地调用c语言.现说明方法,以及注意事项. c程序编译为.so文件: 我们需要的c语言文件为test.c,要从其中调用func(x,y ...

  5. docker mysql 容器报too many connections 引发的liunx磁盘扩容操作

    症状每次删除mysql容器重启没两分钟又报标题错 df -h 命令查看各个挂载空间应用情况发现root home var 三个文件目录挂载的空间满了 网上百度了一下liunx磁盘扩容操作,fdisk ...

  6. Python新手练手项目

    1.新手练手项目集中推荐 https://zhuanlan.zhihu.com/p/22164270 2.Python学习网站 https://www.shiyanlou.com 3.数据结构可视化学 ...

  7. FESCAR

    FESCAR:阿里重磅开源分布式事务解决方案 FESCAR名字的由来:Fast & EaSy Commit And Rollback FESCAR是啥? 被用在微服务架构中的高性能分布式事务解 ...

  8. 史上最详细 Linux 用户与用户组知识

    1.用户和用户组文件 在 linux 中,用户帐号,用户密码,用户组信息和用户组密码均是存放在不同的配置文件中的. 在 linux 系统中,所创建的用户帐号和其相关信息 (密码除外) 均是存放在 / ...

  9. Java第二周总结报告

    第二周的学习,开始正式实践进行Java的学习. 本周做了什么? 了解的Java的一些基本知识,如Java变量,数据类型和运算符等.Java变量对不同的数据类型最好采用不同的命名规则,合理的命名有利于提 ...

  10. Luogu P4602 [CTSC2018]混合果汁

    题目 把果汁按美味度降序排序,以单价为下标插入主席树,记录每个节点的\(sum\)果汁升数和\(val\)果汁总价. 每次询问二分最小美味度,查询美味度大于等于\(mid\)的总体积为\(L\)的最低 ...