题目描述:

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 费用流的更多相关文章

  1. 【2017 ACM/ICPC 乌鲁木齐赛区网络赛环境测试赛 E】蒜头君的排序

    [链接]h在这里写链接 [题意] 在这里写题意 [题解] 莫队算法+树状数组. 区间增加1或减少1. 对逆序对的影响是固定的. (用冒泡排序变成升序的交换次数,就是逆序对的个数) [错的次数] 0 [ ...

  2. Skiing 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛H题(拓扑序求有向图最长路)

    参考博客(感谢博主):http://blog.csdn.net/yo_bc/article/details/77917288 题意: 给定一个有向无环图,求该图的最长路. 思路: 由于是有向无环图,所 ...

  3. 2017青岛赛区网络赛 Smallest Minimum Cut 求最小割的最小割边数

    先最大流跑一遍 在残存网络上把满流边容量+1 非满流边容量设为无穷大 在进行一次最大流即可 (这里的边都不包括建图时用于反悔的反向边) #include<cstdio> #include& ...

  4. hdu 4070 福州赛区网络赛J 贪心 ***

    优先发路程最长的 #include<cstdio> #include<iostream> #include<algorithm> #include<cstri ...

  5. hdu 4049 2011北京赛区网络赛J 状压dp ***

    cl少用在for循环里 #include<cstdio> #include<iostream> #include<algorithm> #include<cs ...

  6. hihocoder #1236 Scores (15北京赛区网络赛J) (五维偏序,强制在线,bitset+分块)

    链接:http://hihocoder.com/problemset/problem/1236 思路; 有n个五维的向量,给出q个询问,每个询问是一个五维向量,问有多少个向量没有一维比这个向量大.并且 ...

  7. luogu 1327 数列排序 & 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 J题 循环节

    luogu 1327 数列排序 题意 给定一个数列\(\{an\}\),这个数列满足\(ai≠aj(i≠j)\),现在要求你把这个数列从小到大排序,每次允许你交换其中任意一对数,请问最少需要几次交换? ...

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

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

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

随机推荐

  1. Buffer和Stream

    Buffer JavaScript 语言自身只有字符串数据类型,没有二进制数据类型.但在处理像TCP流或文件流时,必须使用到二进制数据. 因此在 Node.js中,定义了一个 Buffer 类,该类用 ...

  2. [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)

    LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...

  3. CM+CDH安装教程(CentOS)

    一.简单介绍 CM:Cloudera Manager,Cloudera公司编写的一个CDH的管理后台,类似各CMS的管理后台. CDH:Cloudera’s distribution,includin ...

  4. 牛客网 PAT 算法历年真题 1001 : A+B和C (15)

    1001 : A+B和C (15) 时间限制 1000 ms 内存限制 32768 KB 代码长度限制 100 KB 判断程序 Standard 题目描述 给定区间[-2的31次方, 2的31次方]内 ...

  5. How to import a GitHub project into Eclipse

    Assuming you have created a project in GitHub, these are the steps to import it into Eclipse. First, ...

  6. python(2)之列表

    列表的使用 names=["zhangyang","liming",["sese","popo"],"xiao ...

  7. 【oracle常见错误】ora-00119和ora-00132问题的解决方法

    oracle11g安装后,本地无法登录!前提:服务全部打开,监听也配置好了! win7 64位 oracle 11g 简单的sql命令: 先登录到sqlplus:sqlplus/nolog; 登录数据 ...

  8. iframe子父页面函数互相调用

    1.iframe子页面调用父页面js函数 子页面调用父页面函数只需要写上window.praent就可以了.比如调用a()函数,就写成: window.parent.a();  子页面取父页面中的标签 ...

  9. 3.BIND从服务器及缓存服务器配置

    一.域从服务器 一个域的从服务器(slave)通常是为了备份及负载均衡使用,所有这个域的信息都是由域的主服务器控制,域slave服务器启动时会从域的主服务器(master)上抓取指定域的zone配置文 ...

  10. JDBC:随机生成车牌号,批量插入数据库

    package InsertTest; /* * 单客户端:批量插入 */ import java.sql.Connection; import java.sql.DriverManager; imp ...