题目描述:

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. Nginx反向代理配置教程(php-fpm)

    1.安装nginx http://www.cnblogs.com/lsdb/p/6543441.html 2.安装php-fpm yum install -y php-fpm 3.配置Nginx反向代 ...

  2. WebSphere概要文件的创建与删除

    一.创建单server服务器 /was/bin/manageprofiles.sh -create -profileName server1 \ -profilePath /was/profiles/ ...

  3. 牛客网 PAT 算法历年真题 1009 : 1019. 数字黑洞 (20)

    1019. 数字黑洞 (20) 时间限制 1000 ms 内存限制 32768 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小) 题目描述 给定任一个各位数字不完全相同的4 ...

  4. SpringBoot鸡汤(注解集合)

    1.(ConfigBean.java :是一个带有属性的bean类) @Configuration @ConfigurationProperties(prefix = “com.md”) @Prope ...

  5. Oracle中使用PL/SQL如何定义参数、参数赋值、输出参数和 if 判断

    1.pl/sql如何定义参数 declare --1)定义参数 -- ban_Id number; ban_Name ); 2.pl/sql如何参数赋值 --2)参数赋值-- ban_Id :; ba ...

  6. 运行网站项目时,有时出现Bad Request,该怎么解决?

    有时运行网站项目时,出现Bad Request问题

  7. 交互式shell编程

    FQ #!/usr/bin/env shxfce4-terminal -x sudo python ./local/proxy.py 连续执行 gnome-terminal -x bash -c &q ...

  8. AdaBoost, LogitBoost and GradientBoosting

    前向分步算法与加法模型(forward stagewise algorithm and additive model) (1) AdaBoost:前向分步算法中损失函数取指数损失函数 (2) Logi ...

  9. zookeeper集群环境搭建(纯zookeeper)

    1.首先在三台机子上放上zookeeper的解压包,解压. 然后的话zookeeper是依赖于jdk的,那么也应该安装jdk,这里不详细说明了. mv zookeeper-3.4.5 zookeepe ...

  10. Oracle 11gR2 Database UNDO表空间使用率居高不下处理

    一.UNDO表空间监控图 Prometheus监控的到UNDO表空间使用率超过90%(90%为所有表空间告警阈值).从图中可以看到,多次增加UNDO表空间的DATAFILE,UNDO表空间达到40GB ...