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 乌鲁木齐网络赛 最小费用最大流的更多相关文章

  1. hdu 4411 2012杭州赛区网络赛 最小费用最大流 ***

    题意: 有 n+1 个城市编号 0..n,有 m 条无向边,在 0 城市有个警察总部,最多可以派出 k 个逮捕队伍,在1..n 每个城市有一个犯罪团伙,          每个逮捕队伍在每个城市可以选 ...

  2. BZOJ1834[ZJOI2010]网络扩容——最小费用最大流+最大流

    题目描述 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用. 求:  1.在不扩容的情况下,1到N的最大流:  2.将1到N的最大流增加K所需的最小扩容费用 ...

  3. BZOJ-1834 网络扩容 最小费用最大流+最大流+乱搞

    1834: [ZJOI2010]network 网络扩容 Time Limit: 3 Sec Memory Limit: 64 MB Submit: 2269 Solved: 1136 [Submit ...

  4. BZOJ 1834 Luogu P2604 [ZJOI2010]网络扩容 (最小费用最大流)

    题目连接: (luogu) https://www.luogu.org/problemnew/show/P2604 (bzoj) https://www.lydsy.com/JudgeOnline/p ...

  5. 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 ...

  6. Our Journey of Dalian Ends && Our Journey of Xian Ends 最小费用最大流

    2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛Our Journey of Dalian Ends 题意:要求先从大连到上海,再从上海打西安,中途会经过其他城市,每个城市只能去一次,出一次, ...

  7. 2017乌鲁木齐网络赛 j 题

    题目连接 : https://nanti.jisuanke.com/t/A1256 Life is a journey, and the road we travel has twists and t ...

  8. BZOJ 1834: [ZJOI2010]network 网络扩容(最大流+最小费用最大流)

    第一问直接跑最大流.然后将所有边再加一次,费用为扩容费用,容量为k,再从一个超级源点连一条容量为k,费用为0的边到原源点,从原汇点连一条同样的边到超级汇点,然  后跑最小费用最大流就OK了. ---- ...

  9. bzoj 1834: [ZJOI2010]network 网络扩容【最大流+最小费用最大流】

    第一问直接跑最大流即可.建图的时候按照费用流建,费用为0. 对于第二问,在第一问dinic剩下的残量网络上建图,对原图的每条边(i,j),建(i,j,inf,cij),表示可以用c的花费增广这条路.然 ...

随机推荐

  1. canvas 保存状态

    1.保存和恢复绘图状态: 在绘制图形时,难免会重复使用某个样式,甚至有时会在不同颜色之间来回切换. 那么为了减少代码冗余,我们可以调用画布中的save()方法,来帮我们 保存一些样式和属性,这样我们就 ...

  2. Django系列:(1)PyCharm下创建并运行我们的第一个Django工程

    准备工作: 假设读者已经安装好python 2x或3x,以及安装好Django,以及Pycharm. 我的配置: – Python 2.7.11 – Pycharm Professional 5.0. ...

  3. Android开发——蓝牙

    ---恢复内容开始--- 前言 孤芳自赏,一揽芳华: 人情冷暖,自在人心: 登高远眺,望步止前: 喜笑言开,欺人骗己. 上篇文章介绍了基本的蓝牙使用,书写的demo也不是很完善,希望各位大神能够改正. ...

  4. install nginx error

    the error info : the HTTP gzip module requires the zlib library.You can either disable the module by ...

  5. iOS-UI控件之UIImageView

    contentMode属性 带有scale单词的:图片有可能会拉伸 UIViewContentModeScaleToFill 将图片拉伸至填充整个imageView 图片显示的尺寸跟imageView ...

  6. qt查找框设置

    转载请注明出处:http://www.cnblogs.com/dachen408/p/7229129.html 主界面弹出查找框方法,查找框显示在主界面上层,并还可以点击主界面,非模态. class ...

  7. html5开发移动混合App系列1-开发环境搭建

    最近公司准备开发门店收银系统,是基于IPAD的程序,决定采用基于 Ionic + Cordova + AngularJS技术混合开发模式. 准备 一台mac(安装了mac os的虚拟机也可以),nod ...

  8. iTOP-4412开发板全新升级支持4G全网通模块

    开发板支持4G,GPS,CAN,485,WIFI蓝牙,重力加速度计,陀螺仪等模块. 核心板参数 尺寸:6cm*7cm 高度:连同连接器在内0.26cm CPU:Exynos4412,四核Cortex- ...

  9. 扩展 IHttpModule

    上篇提到请求进入到System.Web后,创建完HttpApplication对象后会执行一堆的管道事件,然后可以通过HttpModule来对其进行扩展,那么这篇文章就来介绍下如何定义我们自己的mod ...

  10. UTF-8,UTF-16

    UTF是 Unicode Translation Format,即把Unicode转做某种格式的意思. 在Unicode基本多文种平面定义的字符(无论是拉丁字母.汉字或其他文字或符号),一律使用2字节 ...