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 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.
题意抽象一下即为要求从A经过B到达C的最短路,并且要求路径上的点不可重复经过。
拆点后点内限流,建立源点S指向B容量为2的一条边,A,C分别建立一条指向汇点T容量为1的边。然后依题意建图即可。
跑一遍费用流便得到答案。
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int MAXN=;
const int maxn=;
const int INF=~0U>>;
int maxflow=,cost=;
int pre[maxn],vis[maxn],dis[maxn];
int S,T,S1;
int n,m;
int tot=;
int pointer[maxn];
map<string,int> mp;
int shanghai;
struct Edge
{
int to,next,cap,f,w;
Edge() {};
Edge(int b,int c,int nxt,int flow,int weight) {to=b,cap=c,next=nxt,f=flow,w=weight;}
}edge[MAXN];
inline void addedge(int a,int b,int c,int w1)
{
edge[tot]=Edge(b,c,pointer[a],,w1);
pointer[a]=tot++;
edge[tot]=Edge(a,,pointer[b],,-w1);
pointer[b]=tot++;
}
bool spfa(int s,int t)
{
queue<int > q;
rep(i,S,T)
{
dis[i]=INF;
vis[i]=false;
pre[i]=-;
}
dis[S]=;
vis[S]=;
q.push(S);
while(!q.empty())
{
int u=q.front();q.pop();
vis[u]=;
for(int j=pointer[u];j!=-;j=edge[j].next)
{
int v=edge[j].to;
// if(u==92) printf("u=92 v=%d\n",v);
if(edge[j].cap-edge[j].f>&&dis[v]>dis[u]+edge[j].w)
{
dis[v]=dis[u]+edge[j].w;
pre[v]=j;
if(!vis[v])
{
vis[v]=;q.push(v);
}
}
}
}
if(pre[T]==-) return false;
else return true;
}
int mcmf()
{
int flow=;
cost=;
while(spfa(S,T))
{
int mi=INF;
for(int i=pre[T];i!=-;i=pre[edge[i^].to])
{
mi=min(mi,edge[i].cap-edge[i].f);
}
for(int i=pre[T];i!=-;i=pre[edge[i^].to])
{
edge[i].f+=mi;
edge[i^].f-=mi;
// printf("edge[%d].w=%d edge[i].to=%d\n",i,edge[i].w,edge[i].to);
cost+=edge[i].w*mi;
}
flow+=mi;
}
return flow;
}
void Input()
{
mp.clear();
scanf("%d",&m);
string a;
string b;
int len;
int cnt=;
memset(pointer,-,sizeof(pointer));
tot=;
rep(i,,m)
{
cin>>a>>b>>len;
if(mp[a]==)
{
mp[a]=cnt;
addedge(cnt,cnt+,,);
cnt+=; }
if(mp[b]==)
{
mp[b]=cnt;
addedge(cnt,cnt+,,);
cnt+=;
}
addedge(mp[a]+,mp[b],INF,len);
addedge(mp[b]+,mp[a],INF,len);
}
S=;T=cnt;
addedge(mp["Shanghai"],mp["Shanghai"]+,,);
addedge(S,mp["Shanghai"],,);
addedge(mp["Xian"]+,T,,);
addedge(mp["Dalian"]+,T,,);
}
int main()
{
freopen("in.txt","r",stdin);
int T;
scanf("%d",&T);
rep(t1,,T)
{
Input();
maxflow=mcmf();
if(maxflow==) printf("%d\n",cost);
else printf("-1\n");
}
return ;
}
2017 乌鲁木齐赛区网络赛 J Our Journey of Dalian Ends 费用流的更多相关文章
- 【2017 ACM/ICPC 乌鲁木齐赛区网络赛环境测试赛 E】蒜头君的排序
[链接]h在这里写链接 [题意] 在这里写题意 [题解] 莫队算法+树状数组. 区间增加1或减少1. 对逆序对的影响是固定的. (用冒泡排序变成升序的交换次数,就是逆序对的个数) [错的次数] 0 [ ...
- Skiing 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛H题(拓扑序求有向图最长路)
参考博客(感谢博主):http://blog.csdn.net/yo_bc/article/details/77917288 题意: 给定一个有向无环图,求该图的最长路. 思路: 由于是有向无环图,所 ...
- 2017青岛赛区网络赛 Smallest Minimum Cut 求最小割的最小割边数
先最大流跑一遍 在残存网络上把满流边容量+1 非满流边容量设为无穷大 在进行一次最大流即可 (这里的边都不包括建图时用于反悔的反向边) #include<cstdio> #include& ...
- hdu 4070 福州赛区网络赛J 贪心 ***
优先发路程最长的 #include<cstdio> #include<iostream> #include<algorithm> #include<cstri ...
- hdu 4049 2011北京赛区网络赛J 状压dp ***
cl少用在for循环里 #include<cstdio> #include<iostream> #include<algorithm> #include<cs ...
- hihocoder #1236 Scores (15北京赛区网络赛J) (五维偏序,强制在线,bitset+分块)
链接:http://hihocoder.com/problemset/problem/1236 思路; 有n个五维的向量,给出q个询问,每个询问是一个五维向量,问有多少个向量没有一维比这个向量大.并且 ...
- luogu 1327 数列排序 & 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 J题 循环节
luogu 1327 数列排序 题意 给定一个数列\(\{an\}\),这个数列满足\(ai≠aj(i≠j)\),现在要求你把这个数列从小到大排序,每次允许你交换其中任意一对数,请问最少需要几次交换? ...
- 2017乌鲁木齐网络赛 j 题
题目连接 : https://nanti.jisuanke.com/t/A1256 Life is a journey, and the road we travel has twists and t ...
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 M. Frequent Subsets Problem【状态压缩】
2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 M. Frequent Subsets Problem 题意:给定N和α还有M个U={1,2,3,...N}的子集,求子集X个数,X满足:X是U ...
随机推荐
- Linux 环境下 网络IO模型
本文讨论的背景是Linux环境下的network IO. IO发生时涉及的对象和步骤: 对于一个network IO (这里我们以read举例),它会涉及到两个系统对象,一个是调用这个IO的proce ...
- [LeetCode] 99. Recover Binary Search Tree(复原BST) ☆☆☆☆☆
Recover Binary Search Tree leetcode java https://leetcode.com/problems/recover-binary-search-tree/di ...
- InnoDB行记录格式(compact)、InnoDB数据页结构
1. compact 行记录格式: 变长字段长度列表,null标志位,记录头信息,列1数据,列2数据 …… 记录头信息中包含许多信息,只列举一部分: 名称 大小 描述 deleted_flag 1bi ...
- nc(netcat)扫描开放端口
探测单个端口是否开放可以用telnet,专业探测端口可以用Nmap,而对于非渗透用途的Linux可以直接用netcat. 1.使用netcat探测端口是否开放 nc -z -v - #z代表不交互要不 ...
- [转]每天一个linux命令(44):top命令
top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器.下面详细介绍它的使用方法.top是一个动态显示过程,即可以通过用户按键来不断刷新 ...
- 批量生成QRcode
本想在excel批量生成GUID,并生成二维码. //Excel生成guid,uuid 格式:600d65bc-948a---fd8dfeebb1cd =LOWER(CONCATENATE(DEC2H ...
- c++ count函数
count函数 algorithm头文件(#include <algorithm>)定义了一个count的函数,其功能类似于find.这个函数使用一对迭代器和一个值做参数,返回这个值出现次 ...
- 加号变空格问题 url参数 post get 请求发送
问题:加号后台接收变空格问题 结论: 1.任何get拼接的请求 参数key value 需要编码后在拼接 2.get请求避免做数据提交,用post提交.jq,axios的post提交默认编码了不会有问 ...
- npm node sass 安装报错
报错为 不能找到python2.7,记得曾经已经安装过python,结果npm install cnpm install npm install node-sass 各种不行,结果在cmd 输入pyt ...
- JQuery操作input
// 不可编辑 $("#id").attr("disabled","disabled"); $("#id").remov ...