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 ...
随机推荐
- DFC-3C和DFC-3B的区别和注意事项
1.Product numbers:WS-F6K-DFC(=)WS-F6K-DFC3A(=)WS-F6K-DFC3B(=)WS-F6K-DFC3BXL(=)WS-F6K-DFC3C(=)WS-F6K- ...
- 给footer标签设置padding:7px auto;失效
margin:auto可以做到水平居中,前提条件就是,这个标签是块状元素,并且有个确定的宽度,百分比的宽度也行: padding的话, 设置成auto它会自动继承浏览器的padding值, 当设置pa ...
- dubbo+zookeeper搭建笔记
参考博客: http://blog.csdn.net/u013142781/article/details/50396621#reply http://blog.csdn.net/u013142781 ...
- toSum
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- 获得APP的包名package和activity
方法一: Aapt dumpbadging xxxx.apk(包的路径) 第一个框为包名 第二个框为主Activity名 方法二: 如果你装了Appium 可以这么操作下 进入设置页,选择APK 路 ...
- 洛谷P1049装箱问题(01背包)
题目描述 有一个箱子容量为VVV(正整数,0≤V≤200000 \le V \le 200000≤V≤20000),同时有nnn个物品(0<n≤300<n \le 300<n≤30, ...
- PostgreSQL日期加减
在PostgreSQL中可以直接对时间进行加减运算:. SELECT now()::timestamp + '1 year'; --当前时间加1年 SELECT now()::timestamp + ...
- Python日期
1. datatime from datetime import datetime, date now = datetime.now() print(now) # 2020-01-20 01:24:0 ...
- Jekyll+Github个人博客构建之路
请参考: http://robotkang.cc/2017/03/HowToCreateBlog/
- Python解决RSA加密
最近爬个网站需要用发现密码是通过RSA加密的,因此找网上python加密例子,发现都没有一个比较完整的demo so,自己写一个吧~ 首先,安装相应的库: 1. pyCrypto : pip inst ...