PAT T1003 Universal Travel Sites
网络流模板~
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
const int inf=1e9;
queue<int> q;
int M,N;
int g[maxn][maxn];
int pre[maxn];
int flow[maxn];
int maxflow;
unordered_map<string,int> pos;
int bfs (int s,int t) {
while (!q.empty()) q.pop();
for (int i=;i<=N;i++) pre[i]=-;
pre[s]=;
q.push(s);
flow[s]=inf;
while (!q.empty()) {
int x=q.front();
q.pop();
if (x==t) break;
for (int i=;i<=N;i++) {
if (g[x][i]>&&pre[i]==-) {
pre[i]=x;
flow[i]=min(flow[x],g[x][i]);
q.push(i);
}
}
}
if (pre[t]==-) return -;
else return flow[t];
}
void Edmonds_Karp (int s,int t) {
int increase=;
while ((increase=bfs(s,t))!=-) {
int k=t;
while (k!=s) {
int last=pre[k];
g[last][k]-=increase;
g[k][last]+=increase;
k=last;
}
maxflow+=increase;
}
}
int main () {
string s1,s2;
cin>>s1>>s2>>M;
pos[s1]=;
pos[s2]=;
int cnt=,x;
for (int i=;i<M;i++) {
cin>>s1>>s2>>x;
if (pos[s1]==) pos[s1]=cnt++;
if (pos[s2]==) pos[s2]=cnt++;
g[pos[s1]][pos[s2]]=x;
}
N=cnt-;
Edmonds_Karp (,);
printf ("%d",maxflow);
return ;
}
PAT T1003 Universal Travel Sites的更多相关文章
- PAT 甲级 1030 Travel Plan (30 分)(dijstra,较简单,但要注意是从0到n-1)
1030 Travel Plan (30 分) A traveler's map gives the distances between cities along the highways, to ...
- PAT A 1030. Travel Plan (30)【最短路径】
https://www.patest.cn/contests/pat-a-practise/1030 找最短路,如果有多条找最小消耗的,相当于找两次最短路,可以直接dfs,数据小不会超时. #incl ...
- PAT 甲级 1030 Travel Plan
https://pintia.cn/problem-sets/994805342720868352/problems/994805464397627392 A traveler's map gives ...
- PAT甲级——A1030 Travel Plan
A traveler's map gives the distances between cities along the highways, together with the cost of ea ...
- PAT Advanced 1030 Travel Plan (30) [Dijkstra算法 + DFS,最短路径,边权]
题目 A traveler's map gives the distances between cities along the highways, together with the cost of ...
- 后台调用前台JS(查看客户端IE版本)
1.前端代码 </form> //注意放在form下面<script> function readRegedit() { var obj = n ...
- PAT 1030 Travel Plan[图论][难]
1030 Travel Plan (30)(30 分) A traveler's map gives the distances between cities along the highways, ...
- PAT (Advanced Level) 1030. Travel Plan (30)
先处理出最短路上的边.变成一个DAG,然后在DAG上进行DFS. #include<iostream> #include<cstring> #include<cmath& ...
- PAT甲题题解-1030. Travel Plan (30)-最短路+输出路径
模板题最短路+输出路径如果最短路不唯一,输出cost最小的 #include <iostream> #include <cstdio> #include <algorit ...
随机推荐
- float和position:absolute脱离文本流的区别
float和position:absolute脱离文本流的区别原创 paediatrician 最后发布于2016-09-19 10:43:05 阅读数 5375 收藏展开 文档流:将窗体自上而下分成 ...
- IDEA 自定义注释模板 支持设置多个param参数
在使用IDEA过程中,很多人可能感觉自带注释太简约了,想增加一些属性,比如作者.创建时间.版本号等等,这个时候我们可以使用自定义的注释模板来实现我们需求,话不多说直接进入如何自定义模板设置: 打开设置 ...
- 理解Javascript的原型和原型链
前言 本文2088字,阅读大约需要13分钟. 总括: 结合实例阐述了原型和原型链的概念并总结了几种创建对象的方法,扩展原型链的方法. 参考文章:The Secret Life of Objects,继 ...
- Spring Boot整合EhCache
本文讲解Spring Boot与EhCache的整合. 1 EhCache简介 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认CacheProvid ...
- js加密(七)steam登录
1. url: https://store.steampowered.com/login/?redir=&redir_ssl=1 2. target: 登录 3. 分析 3.1 老样子,抓包, ...
- 安卓开发:Android Studio自动import
我只想说,真好用!哈哈,提高效率的好东西. 参考: [https://blog.csdn.net/pjdd123/article/details/80953669] [https://www.cnbl ...
- Django--模型管理器
参考https://blog.csdn.net/qq_34788903/article/details/87889451 可参考视频 : https://www.bilibili.com/video ...
- DataGrid 獲取 制定 row Col 單元格
public static class DataGridHelper { /// <summary> /// Gets the v ...
- UE4高级运动系统(Advanced Locomotion System V3)插件分析
Advanced Locomotion System V3是虚幻商城的一款第三方插件.它相比UE4的基础走跑跳表现,实现了更多动作游戏里常用的运动特性,虽然价格定价不菲,依然备受关注.笔者试用了这款插 ...
- input输入框type设置为number,maxlength无效
一个小细节,以前很少注意,直到最近做的一个项目,当我把一个输入数字的input框的类型设置为number时,input框还是可以无限输入,设置maxlengh=10就不起作用了.然后我就去百度了一下, ...