链接:

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. 转载 STM32 使用Cubemx 建一个USB(HID)设备下位机,实现数据收发

    STM32 使用Cubemx 建一个USB(HID)设备下位机,实现数据收发  本文转载自 https://www.cnblogs.com/xingboy/p/9913963.html 这里我主要说一 ...

  2. delphi TDbGrid 右键 PopupMenu 菜单只在有数据的地方弹出

    最近用delphi做开发,用到了DbGrid控件,想在控件上点击鼠标右键弹出菜单 关联DbGrid的 Popupmenu 倒是可以实现,但是这样的效果是不管你在哪里单击鼠标右键 只要在DBGrid里面 ...

  3. centos7.5搭建svn

    1.安装svnyum install subversion 2.查看安装位置rpm -ql subversion 3.创建svn版本库目录mkdir -p /var/svn/svnrepos 4.创建 ...

  4. Private Variable

    Any variable defined inside a function is considered private since it is inaccessable outside that f ...

  5. git clone 指定分支操作

    服务器迁移,而且原来本地开发是在同一个目录中切换不同的分支,感觉有点挫,于是打算一个文件目录对应一个分支,这样不会有太大的文件差异. 记录下来本次操作,可能以后还会用到. git初始化一般是这样. g ...

  6. 网格UV展开

    原文链接 UV展开是什么 参数曲面的参数域变量一般用UV字母来表达,比如参数曲面F(u,v).所以一般叫的三维曲面本质上是二维的,它所嵌入的空间是三维的.凡是能通过F(u,v)来表达的曲面都是参数曲面 ...

  7. Qt - 获取本机网络信息

    目的: 获取本机的主机名.IP地址.硬件地址等网络信息. 工具: 使用Qt提供的网络模块QtNetwork(pro文件里面加network): 使用Qt提供的类QHostInfo.QNetworkIn ...

  8. etcd租约机制

    新建租约 新建一个过期时间为120s的租约 # etcdctl lease grant lease 018f6d7bb11aba0d granted with TTL(120s) 查看新建的租约信息 ...

  9. 【6.24校内test】T2 不老梦

    [题目背景] 于万人中万幸得以相逢,刹那间澈净明通. 成为我所向披靡的勇气和惶恐,裂山海,堕苍穹. 爱若执炬迎风,炽烈而哀恸,诸般滋味皆在其中. 韶华宛转吟诵,苍凉的光荣,急景凋年深情难共. ——银临 ...

  10. Two progressions CodeForces - 125D (暴力)

    大意: 给定序列, 求划分为两个非空等差序列. 暴搜, 加个记忆化剪枝. #include <iostream> #include <sstream> #include < ...