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的花费增广这条路.然 ...
随机推荐
- laravel 学习
解决办法:没有开启PHP.ini的fileinfo扩展
- jQuery相关知识总结
1 encodeURIComponent(city)处理js传值乱码问题 2 总体概述 以后项目如果没有特殊情况,一般采用jQuery作为最基础的公共底层库. 另外对于前端的javascript相关的 ...
- Bmob使用心得
1.在 Project 的 build.gradle 文件中添加 Bmob的maven仓库地址,示例如下:(注意文字说明部分): allprojects { repositories { jcente ...
- qt5.8 链接mysql错误:driver not load
转载请注明出处:http://www.cnblogs.com/dachen408/p/7155858.html 问题:qt5.8 链接mysql错误:driver not load. 解决方案:1.安 ...
- CAD参数绘制图案填充(网页版)
绘制工程图,常常需要将某种图案填充到某一区域,例如剖面线的绘制.MxCAD提供了丰富的填充图案,可以利用这些图案进行快速填充. js中实现代码说明: function DrawPathToHatch2 ...
- CAD使用DeleteXData删除数据(com接口)
主要用到函数说明: MxDrawEntity::DeleteXData 删除扩展数据,详细说明如下: 参数 说明 pzsAppName 删除的扩展数据名称,如果为空,删除所有扩展数据 c#代码实现如下 ...
- Java基础(十四)--装箱、拆箱详解
Java中基本数据类型都有相对应的包装类 什么是装箱?什么是拆箱? 在Java SE5之前,Integer是这样初始化的 Integer i = new Integer(10); 而在从Java SE ...
- vlmcsd-1111-2017-06-17
Source and binaries: http://rgho.st/6c6R7RwMZ 全部编译好了 https://www.upload.ee/files/7131474/vlmcsd-11 ...
- pymouse pykeyboard
import time from pymouse import PyMouse from pykeyboard import PyKeyboard import re import win32clip ...
- 导航栏 active 跟随鼠标效果
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...