链接:

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. vue中html、css、js 分离

    在正常的创建和引用vue文件都是html.css.js三者在一起的,这样写起来虽然方便了,但是页面比较大或者代码比较多的情况下,即使使用组件有时代码也比较多,简单来说查找不变不利于编程,大的来说影像优 ...

  2. 慕课网_Java入门第三季

    第1章 异常与异常处理 1-1 Java异常简介 (06:50) 1-2 Java中使用try..catch..finally实现异常处理 (05:08) import java.util.Input ...

  3. springmvc 使用Jackson框架的配置

    <!--start:使用Jackson 1.x的配置,需要导入的jar包:jackson-core-lpgl-xxx.jar.jackson-mapper-lgpl-xxx.jar --> ...

  4. 用番茄工作法提升工作效率 (四)ToDoList的持续优化

    一.写在前面 前面三篇文章,系统介绍了我如何使用番茄工作法,并结合“自制”的桌面ToDoList工具来实现自己的任务管理. 自制ToDoList的初衷是自我管理,但是好友看到我的桌面(程序)后,建议我 ...

  5. const char* to char*(当函数传递参数时)

    来自 https://blog.csdn.net/rongrongyaofeiqi/article/details/52442169 https://blog.csdn.net/hebbely/art ...

  6. 【Qt开发】在QLabel已经显示背景图片后绘制图形注意事项

    主要是要解决图形覆盖的问题,通常的办法就是对QLabel进行子类化,并重载函数: void myLabel::paintEvent(QPaintEvent *event)   {       QLab ...

  7. 深入理解java:3. NIO 编程

    I/O简介 I/O即输入输出,是计算机与外界世界的一个借口. IO操作的实际主题是操作系统. 在Java编程中,一般使用流的方式来处理IO,所有的IO都被视作是单个字节的移动,通过stream对象一次 ...

  8. 深入理解java:2.3.5. 并发编程concurrent包 之容器BlockingQueue(阻塞队列)

    1. 什么是阻塞队列? 阻塞队列(BlockingQueue)是一个支持两个附加操作的队列. 这两个附加的操作是:在队列为空时,获取元素的线程会等待队列变为非空. 当队列满时,存储元素的线程会等待队列 ...

  9. 第七次学习总结&&第五次实验报告

    一.实验目的 (1)理解抽象类与接口的使用: (2)了解包的作用,掌握包的设计方法. 二.实验要求 (1)掌握使用抽象类的方法. (2)掌握使用系统接口的技术和创建自定义接口的方法. (3)了解 Ja ...

  10. Connection is read-only. Queries leading to data modification are not allowed 错误原因

    因为我再spring 中使用了AOP进行事务管理,有如下配置 <tx:advice id="txAdvice" transaction-manager="trans ...