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 ...
随机推荐
- [NOIP 2015TG D1T3] 斗地主
题目描述 牛牛最近迷上了一种叫斗地主的扑克游戏.斗地主是一种使用黑桃.红心.梅花.方片的A到K加上大小王的共54张牌来进行的扑克牌游戏.在斗地主中,牌的大小关系根据牌的数码表示如下:3<4< ...
- python, 在信用评级中,计算KS statistic值
# -*- coding: utf-8 -*- import pandas as pd from sklearn.grid_search import GridSearchCV from sklear ...
- DLL的Export和Import及extern "C"
今天使用Unrar.dll,在调用RARProcessFileW时,VS总是提示“error LNK2001: 无法解析的外部符号”. Unrar.dll中是使用 extern "C&quo ...
- HashMap与TreeMap按照key和value排序
package com.sort; import java.util.ArrayList; import java.util.Collections; import java.util.Compara ...
- 使用laravel搭建CURD后台页面
配置即一切 一切皆于需求,后台从0开始搭建,但是写了一两个页面后发现太多的是对单表的增删改查操作,于是就想到了,能不能做一个快速搭建的后台.想到一句话,配置即一切.如果一个CURD后台能只进行配置就自 ...
- java⑥
import java.util.Scanner; /** * 所有在java.lang包下面的所有类 不需要显示的引入包! * java.util.Scanner : 想获取用户的输入 必须引入相关 ...
- SpringBoot + JPA 连接MySQL完整实例(一)
开发工具 1.Eclipse 2.Maven 3.Spring Boot 首先,Eclipse中配置好maven,具体请百度 工程结构: 实现步骤: 1.Eclipse中新建一个maven proje ...
- 4.5 C++重载、覆盖和遮蔽
参考:http://www.weixueyuan.net/view/6375.html 总结: 函数签名包括函数名和函数参数的个数.顺序以及参数数据类型. 需要注意的是函数签名并不包含函数返回值部分, ...
- 2.5 C++类class和结构体struct区别
参考:http://www.weixueyuan.net/view/6337.html 总结: 在C++中,struct类似于class,在其中既可以定义数据成员,又可以定义成员函数. 在C++中,s ...
- pssac plot
for multi-waveforms with different colors: R-Xmin/-Xmax/Ymin/Ymax -Ba/b, a: x delta; b:y delta -MYma ...