Our Journey of Dalian Ends 乌鲁木齐网络赛 最小费用最大流
Life is a journey, and the road we travel has twists and turns, which sometimes lead us to unexpected places and unexpected people.
Now our journey of Dalian ends. To be carefully considered are the following questions.
Next month in Xian, an essential lesson which we must be present had been scheduled.
But before the lesson, we need to attend a wedding in Shanghai.
We are not willing to pass through a city twice.
All available expressways between cities are known.
What we require is the shortest path, from Dalian to Xian, passing through Shanghai.
Here we go.
Input Format
There are several test cases.
The first line of input contains an integer tt which is the total number of test cases.
For each test case, the first line contains an integer m~(m\le 10000)m (m≤10000) which is the number of known expressways.
Each of the following mm lines describes an expressway which contains two string indicating the names of two cities and an integer indicating the length of the expressway.
The expressway connects two given cities and it is bidirectional.
Output Format
For eact test case, output the shortest path from Dalian to Xian, passing through Shanghai, or output -1−1 if it does not exist.
样例输入
3
2
Dalian Shanghai 3
Shanghai Xian 4
5
Dalian Shanghai 7
Shanghai Nanjing 1
Dalian Nanjing 3
Nanjing Xian 5
Shanghai Xian 8
3
Dalian Nanjing 6
Shanghai Nanjing 7
Nanjing Xian 8
样例输出
7
12
-1 保证每个点只过一次的条件,把每个点拆成入点和出点,容量1,费用0
在点之间加双向边,容量INF,费用为距离
在原点和Shanghai之间加边,容量2,费用0
在Dalian和Xian向终点加边,容量1,费用0 在这里注意每个点只过一次的条件不包括上海,也就是说上海入点和出点之间的容量要设置为2
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<deque>
#include<iomanip>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<functional>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL; const int MAXN = ;
const int MAXM = * + ;
const int INF = 0x3f3f3f3f;
struct Edge
{
int to, next, cap, flow, cost;
}edge[MAXM];
int head[MAXN], tol;
int pre[MAXN], dis[MAXN];
bool vis[MAXN];
int N;//节点总个数,节点编号从0~N-1
void init()
{
//N = n;
tol = ;
memset(head, -, sizeof(head));
}
void addedge(int u, int v, int cap, int cost)
{
edge[tol].to = v;
edge[tol].cap = cap;
edge[tol].cost = cost;
edge[tol].flow = ;
edge[tol].next = head[u];
head[u] = tol++;
edge[tol].to = u;
edge[tol].cap = ;
edge[tol].cost = -cost;
edge[tol].flow = ;
edge[tol].next = head[v];
head[v] = tol++;
}
bool spfa(int s, int t)
{
queue<int>q;
for (int i = ; i < N; i++)
{
dis[i] = INF;
vis[i] = false;
pre[i] = -;
}
dis[s] = ;
vis[s] = true;
q.push(s);
while (!q.empty())
{
int u = q.front();
q.pop();
vis[u] = false;
for (int i = head[u]; i != -; i = edge[i].next)
{
int v = edge[i].to;
if (edge[i].cap > edge[i].flow &&
dis[v] > dis[u] + edge[i].cost)
{
dis[v] = dis[u] + edge[i].cost;
pre[v] = i;
if (!vis[v])
{
vis[v] = true;
q.push(v);
}
}
}
}
if (pre[t] == -)return false;
else return true;
}
//返回的是最大流,cost存的是最小费用
int minCostMaxflow(int s, int t, LL &cost)
{
int flow = ;
cost = ;
while (spfa(s, t))
{
int Min = INF;
for (int i = pre[t]; i != -; i = pre[edge[i ^ ].to])
{
if (Min > edge[i].cap - edge[i].flow)
Min = edge[i].cap - edge[i].flow;
}
for (int i = pre[t]; i != -; i = pre[edge[i ^ ].to])
{
edge[i].flow += Min;
edge[i ^ ].flow -= Min;
cost += edge[i].cost * Min;
}
flow += Min;
}
return flow;
} int main()
{
map<string, int> m;
int T, cnt, dis;
ios::sync_with_stdio(false);
cin >> T;
string f, t;
while (T--)
{
init();
int k;
cin >> k;
m.clear();
cnt = ;
vector<pair<int, int>> v;
vector<int> d;
for (int i = ; i < k; i++)
{
cin >> f >> t >> dis;
if (m.find(f) == m.end())
{
m[f] = cnt++;
}
if (m.find(t) == m.end())
{
m[t] = cnt++;
}
v.push_back(make_pair(m[f], m[t]));
d.push_back(dis);
}
if (m.find("Shanghai") == m.end() || m.find("Xian") == m.end() || m.find("Dalian") == m.end())
{
cout << - << endl;
continue;
}
int p = m["Shanghai"];
for (int i = ; i < k; i++)
{
int _u = v[i].first, _v = v[i].second, _d = d[i];
addedge(_u + cnt, _v, INF, _d);
addedge(_v + cnt, _u, INF, _d);
}
for (int i = ; i < cnt; i++)
{
if (i != p)
addedge(i, i + cnt, , );
else
addedge(i, i + cnt, , );
}
int st = * cnt, ed = * cnt + ;
N = * cnt + ;
int S = m["Shanghai"], X = m["Xian"], D = m["Dalian"];
addedge(st, S, , );
addedge(D + cnt, ed, , );
addedge(X + cnt, ed, , );
LL tmp;
int ans = minCostMaxflow(st, ed, tmp);
if (ans != )
cout << - << endl;
else
cout << tmp << endl;
}
}
Our Journey of Dalian Ends 乌鲁木齐网络赛 最小费用最大流的更多相关文章
- hdu 4411 2012杭州赛区网络赛 最小费用最大流 ***
题意: 有 n+1 个城市编号 0..n,有 m 条无向边,在 0 城市有个警察总部,最多可以派出 k 个逮捕队伍,在1..n 每个城市有一个犯罪团伙, 每个逮捕队伍在每个城市可以选 ...
- BZOJ1834[ZJOI2010]网络扩容——最小费用最大流+最大流
题目描述 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用. 求: 1.在不扩容的情况下,1到N的最大流: 2.将1到N的最大流增加K所需的最小扩容费用 ...
- BZOJ-1834 网络扩容 最小费用最大流+最大流+乱搞
1834: [ZJOI2010]network 网络扩容 Time Limit: 3 Sec Memory Limit: 64 MB Submit: 2269 Solved: 1136 [Submit ...
- BZOJ 1834 Luogu P2604 [ZJOI2010]网络扩容 (最小费用最大流)
题目连接: (luogu) https://www.luogu.org/problemnew/show/P2604 (bzoj) https://www.lydsy.com/JudgeOnline/p ...
- 2017 乌鲁木齐赛区网络赛 J Our Journey of Dalian Ends 费用流
题目描述: Life is a journey, and the road we travel has twists and turns, which sometimes lead us to une ...
- Our Journey of Dalian Ends && Our Journey of Xian Ends 最小费用最大流
2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛Our Journey of Dalian Ends 题意:要求先从大连到上海,再从上海打西安,中途会经过其他城市,每个城市只能去一次,出一次, ...
- 2017乌鲁木齐网络赛 j 题
题目连接 : https://nanti.jisuanke.com/t/A1256 Life is a journey, and the road we travel has twists and t ...
- BZOJ 1834: [ZJOI2010]network 网络扩容(最大流+最小费用最大流)
第一问直接跑最大流.然后将所有边再加一次,费用为扩容费用,容量为k,再从一个超级源点连一条容量为k,费用为0的边到原源点,从原汇点连一条同样的边到超级汇点,然 后跑最小费用最大流就OK了. ---- ...
- bzoj 1834: [ZJOI2010]network 网络扩容【最大流+最小费用最大流】
第一问直接跑最大流即可.建图的时候按照费用流建,费用为0. 对于第二问,在第一问dinic剩下的残量网络上建图,对原图的每条边(i,j),建(i,j,inf,cij),表示可以用c的花费增广这条路.然 ...
随机推荐
- SQL SERVER 事务例子
存储过程格式: CREATE PROCEDURE YourProcedure AS BEGIN SET NOCOUNT ON; BEGIN TRY---------------------开始捕捉异常 ...
- Burp Suite集成sqlmap
本地环境 JDK1.8 Burp Suite 1.7.26 Python2.7 64位安装包 sqlmap zip包 安装python及sqlmap python下载下来默认安装即可,配置系统环境变量 ...
- Shiro 自定义登陆、授权、拦截器
Shiro 登陆.授权.拦截 按钮权限控制 一.目标 Maven+Spring+shiro 自定义登陆.授权 自定义拦截器 加载数据库资源构建拦截链 使用总结: 1.需要设计的数据库:用户.角色.权限 ...
- git ---查看工作状态和历史提交
1.git查看状态 -git status 2.版权声明 版权声明:新建一个 LICENSE.txt 文件 开源协议:MIT //开源许可里面的最宽松的一个协议,别人可以随便用你的代码,但 ...
- A Convolution Tree with Deconvolution Branches: Exploiting Geometric Relationships for Single Shot Keypoint Detection
作者:嫩芽33出处:http://www.cnblogs.com/nenya33/p/6817781.html 版权:本文版权归作者和博客园共有 转载:欢迎转载,但未经作者同意,必须保留此段声明:必须 ...
- ROS在rviz中实时显示轨迹(nav_msgs/Path消息的使用)
消息结构说明nav_msgs/Path.msg结构#An array of poses that represents a Path for a robot to followHeader heade ...
- shellinabox的安装使用
一.简介 Shell In A Box(发音是shellinabox)是一款基于Web的终端模仿器,由Markus Gutschke开辟而成.它有内置的Web办事器,在指定的端口上作为一个基于Web的 ...
- EditControl 限制输入文本的三种方法
下边是三种限制编辑框输入内容的方法.在VS里建立基于对话框的应用程序CMyEdit,打开资源视图,删除对话框上自带的确定和取消按钮.然后添加一编辑框控件,将其ID修改为IDC_MY_EDIT,通过类向 ...
- ansible相关说明
2.ansible相关说明 2.1.ansible相关命令 ansible:定义并运行简单任务,主要执行ad-hoc命令 ansible-config:查看.编辑.管理ansible配置 ansibl ...
- 无插件纯Web 3D机房,HTML5+WebGL倾力打造
前言 最近项目开发任务告一段落,刚好有时间整理这大半年的一些成果.使用html5时间还不久,对js的认识还不够深入.没办法,以前一直搞java,对js的一些语言特性和概念一时还转换不过来. 上一篇大数 ...