【2017新疆网络赛】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≤10000)m (m10000) 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 −11if it 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));
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值