【2017新疆网络赛】Our Journey of Dalian Ends 费用流
2017-09-22 20:34
381 查看
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.
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≤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.
For eact test case, output the shortest path from Dalian to Xian, passing through Shanghai, or output −1−1if
it does not exist.
题意:
给定若干个城市,出发点为大连,目的地为西安,但是要求中途必须经过上海,并且图中每个城市只能经过一次,给出m条路(双向道路),走第i条路需要wi代价,求所有满足要求的方案中花费的最小代价,如果没有满足的方案,输出-1。
思路:
这题其实是hdu2686的变种http://blog.csdn.net/zchahaha/article/details/51503242
遇到只能经过一次这个条件就可以想到拆后跑费用流了,,,模版题。
//
// main.cpp
// J
//
// Created by zc on 2017/9/21.
// Copyright © 2017年 zc. All rights reserved.
//
#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<vector>
#define ll long long
using namespace std;
const ll INF=1e17+1;
map<string,int>mp;
string s1,s2;
const int maxv = 55000;
const int maxe = 110000;
typedef ll Type;
struct Edge {
int u, v;
Type cap, flow, cost;
Edge() {}
Edge(int u, int v, Type cap, Type flow, Type cost) {
this->u = u;
this->v = v;
this->cap = cap;
this->flow = flow;
this->cost = cost;
}
};
struct MCFC {
int n, m, s, t;
Edge edges[maxe];
int first[maxv];
int next[maxe];
int inq[maxv];
Type d[maxv];
int p[maxv];
Type a[maxv];
int Q[maxe];
void init(int n) {
this->n = n;
memset(first, -1, sizeof(first));
m = 0;
}
void AddEdge(int u, int v, Type cap, Type cost) {
edges[m] = Edge(u, v, cap, 0, cost);
next[m] = first[u];
first[u] = m++;
edges[m] = Edge(v, u, 0, 0, -cost);
next[m] = first[v];
first[v] = m++;
}
bool BellmanFord(int s, int t, Type &flow, Type &cost) {
for (int i = 0; i < n; i++) d[i] = INF;
memset(inq, false, sizeof(inq));
d[s] = 0; inq[s] = true; p[s] = 0; a[s] = INF;
int front, rear;
Q[rear = front = 0] = s;
while (front <= rear) {
int u = Q[front++];
inq[u] = false;
for (int i = first[u]; i != -1; i = next[i]) {
Edge& e = edges[i];
if (e.cap > e.flow && d[e.v] > d[u] + e.cost) {
d[e.v] = d[u] + e.cost;
p[e.v] = i;
a[e.v] = min(a[u], e.cap - e.flow);
if (!inq[e.v]) {Q[++rear] = e.v; inq[e.v] = true;}
}
}
}
if (d[t] == INF) return false;
flow += a[t];
cost += d[t] * a[t];
int u = t;
while (u != s) {
edges[p[u]].flow += a[t];
edges[p[u]^1].flow -= a[t];
u = edges[p[u]].u;
}
return true;
}
Type Mincost(int s, int t) {
Type flow = 0, cost = 0;
while (BellmanFord(s, t, flow, cost));
if(flow!=2) return -1;
return cost;
}
}H;
int main(int argc, const char * argv[]) {
std::ios::sync_with_stdio(false);
int T,n;
cin>>T;
while(T--)
{
cin>>n;
mp.clear();
int m=0;
H.init(4*n+4);
int g=n+n;
for(int i=0;i<n;i++)
{
ll c;
cin>>s1>>s2>>c;
if(mp.find(s1)==mp.end())
{
mp[s1]=++m;
H.AddEdge(mp[s1], mp[s1]+g, 1, 0);
}
if(mp.find(s2)==mp.end())
{
mp[s2]=++m;
H.AddEdge(mp[s2], mp[s2]+g, 1, 0);
}
int x=mp[s1],y=mp[s2];
H.AddEdge(x+g, y, 1, c);
H.AddEdge(y+g, x, 1, c);
}
int x=mp["Shanghai"],y=mp["Dalian"],z=mp["Xian"];
if(x==0||y==0||z==0)
{
printf("-1\n");
continue;
}
int s=0,t=x+g;
H.AddEdge(s, y, 1, 0);
H.AddEdge(s, z, 1, 0);
H.AddEdge(x, t, 1, 0);
printf("%lld\n",H.Mincost(s, t));
}
}
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≤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−1ifit 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
题意:
给定若干个城市,出发点为大连,目的地为西安,但是要求中途必须经过上海,并且图中每个城市只能经过一次,给出m条路(双向道路),走第i条路需要wi代价,求所有满足要求的方案中花费的最小代价,如果没有满足的方案,输出-1。
思路:
这题其实是hdu2686的变种http://blog.csdn.net/zchahaha/article/details/51503242
遇到只能经过一次这个条件就可以想到拆后跑费用流了,,,模版题。
//
// main.cpp
// J
//
// Created by zc on 2017/9/21.
// Copyright © 2017年 zc. All rights reserved.
//
#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<vector>
#define ll long long
using namespace std;
const ll INF=1e17+1;
map<string,int>mp;
string s1,s2;
const int maxv = 55000;
const int maxe = 110000;
typedef ll Type;
struct Edge {
int u, v;
Type cap, flow, cost;
Edge() {}
Edge(int u, int v, Type cap, Type flow, Type cost) {
this->u = u;
this->v = v;
this->cap = cap;
this->flow = flow;
this->cost = cost;
}
};
struct MCFC {
int n, m, s, t;
Edge edges[maxe];
int first[maxv];
int next[maxe];
int inq[maxv];
Type d[maxv];
int p[maxv];
Type a[maxv];
int Q[maxe];
void init(int n) {
this->n = n;
memset(first, -1, sizeof(first));
m = 0;
}
void AddEdge(int u, int v, Type cap, Type cost) {
edges[m] = Edge(u, v, cap, 0, cost);
next[m] = first[u];
first[u] = m++;
edges[m] = Edge(v, u, 0, 0, -cost);
next[m] = first[v];
first[v] = m++;
}
bool BellmanFord(int s, int t, Type &flow, Type &cost) {
for (int i = 0; i < n; i++) d[i] = INF;
memset(inq, false, sizeof(inq));
d[s] = 0; inq[s] = true; p[s] = 0; a[s] = INF;
int front, rear;
Q[rear = front = 0] = s;
while (front <= rear) {
int u = Q[front++];
inq[u] = false;
for (int i = first[u]; i != -1; i = next[i]) {
Edge& e = edges[i];
if (e.cap > e.flow && d[e.v] > d[u] + e.cost) {
d[e.v] = d[u] + e.cost;
p[e.v] = i;
a[e.v] = min(a[u], e.cap - e.flow);
if (!inq[e.v]) {Q[++rear] = e.v; inq[e.v] = true;}
}
}
}
if (d[t] == INF) return false;
flow += a[t];
cost += d[t] * a[t];
int u = t;
while (u != s) {
edges[p[u]].flow += a[t];
edges[p[u]^1].flow -= a[t];
u = edges[p[u]].u;
}
return true;
}
Type Mincost(int s, int t) {
Type flow = 0, cost = 0;
while (BellmanFord(s, t, flow, cost));
if(flow!=2) return -1;
return cost;
}
}H;
int main(int argc, const char * argv[]) {
std::ios::sync_with_stdio(false);
int T,n;
cin>>T;
while(T--)
{
cin>>n;
mp.clear();
int m=0;
H.init(4*n+4);
int g=n+n;
for(int i=0;i<n;i++)
{
ll c;
cin>>s1>>s2>>c;
if(mp.find(s1)==mp.end())
{
mp[s1]=++m;
H.AddEdge(mp[s1], mp[s1]+g, 1, 0);
}
if(mp.find(s2)==mp.end())
{
mp[s2]=++m;
H.AddEdge(mp[s2], mp[s2]+g, 1, 0);
}
int x=mp[s1],y=mp[s2];
H.AddEdge(x+g, y, 1, c);
H.AddEdge(y+g, x, 1, c);
}
int x=mp["Shanghai"],y=mp["Dalian"],z=mp["Xian"];
if(x==0||y==0||z==0)
{
printf("-1\n");
continue;
}
int s=0,t=x+g;
H.AddEdge(s, y, 1, 0);
H.AddEdge(s, z, 1, 0);
H.AddEdge(x, t, 1, 0);
printf("%lld\n",H.Mincost(s, t));
}
}
相关文章推荐
- 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 - J Our Journey of Dalian Ends(最小费用最大流)
- 【2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛】 J Our Journey of Dalian Ends 【拆点费用流】
- 计蒜客-乌鲁木齐网络赛&费用流&拆点-Our Journey of Dalian Ends
- 计蒜客 16959 Our Journey of Dalian Ends(2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 J)
- 计蒜客-2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛-J-Our Journey of Dalian Ends
- 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 J.Our Journey of Dalian Ends【最小费用最大流】
- 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 [计蒜客] Our Journey of Dalian Ends
- 计蒜客:2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛:Our Journey of Dalian Ends
- 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 J. Our Journey of Dalian Ends [网络流]
- ACM ICPC 乌鲁木齐网络赛 J. Our Journey of Dalian Ends
- Our Journey of Dalian Ends 乌鲁木齐网络赛 最小费用最大流
- 计蒜客 Our Journey of Dalian Ends(17新疆网赛) 费用流(思维建图)
- Our Journey of Dalian Ends (最小费用最大流)
- 2017 ACM 区域赛青岛站(现场赛) K Our Journey of Xian Ends
- 2017ICPC青岛K Our Journey of Xian Ends
- 计蒜客 16959 Our Journey of Dalian Ends(最小费用最大流-mcmf)
- Our Journey of Dalian Ends
- 计蒜客 Our Journey of Dalian Ends 拆点+最小费用最大流
- Our Journey of Dalian Ends【网络流】
- J: Our Journey of Dalian Ends