计蒜客-乌鲁木齐网络赛&费用流&拆点-Our Journey of Dalian Ends

https://nanti.jisuanke.com/t/16959
给定一个图,问你从 西安出发,经过上海,到达大连,最小的花费是多少。要求每一个城市只能经过一次。。
一直在想能不能改dij。。。
正确思路:建立一个网络流&拆点。s连接 西安和大连的入点,上海的拆点连接 上海。跑一遍费用流。
注意 每个边都是可以过好多次的,所以边的权为inf。。
但是每个点只能跑一次,所以点的是1.

#include <iostream>
#include<queue>
#include<cstring>
#include <cstdio>
#include <map>
using namespace std;
typedef long long ll;
typedef int Type;
const int N=12000*4;
const Type inf=0x3f3f3f3f;
struct edge{
  int u,v;
  Type cap, flow, cost;
  edge() {}
  edge(int from,int to,int c,int f,int co):u(from),v(to),cap(c),flow(f),cost(co){}
};
map<string,int>mp;
struct MCMF{
  int n,m;
  vector<edge>es;
  vector<int>G[N];
  int inq[N];//是否在队列中
  Type d[N];//SPFA时总费用
  int p[N];//上一条弧
  Type a[N];//可改进量

  void init(int n){
    this->n=n;
    for(int i=0; i<n; i++)G[i].clear();
    es.clear();
  }
  void add_edge(int u,int v,Type cap,Type cost){
    es.push_back(edge(u, v, cap, 0, cost));
    es.push_back(edge(v, u, 0, 0, -cost));
    m=es.size();
    G[u].push_back(m-2);
    G[v].push_back(m-1);
  }

    bool spfa(int s,int t,Type& flow,Type& cost){
    memset(inq, 0, sizeof(inq));
    for(int i=0; i<n; i++)d[i]=inf;
    d[s]=0;inq[s]=1;p[s]=0;a[s]=inf;

    queue<int>q;
    q.push(s);
    while(!q.empty()){
      int u=q.front();q.pop();inq[u]=0;
      for(int i=0; i<G[u].size(); i++){
        edge& e=es[G[u][i]];
        int v=e.v;
        if(e.cap>e.flow && d[v]>d[u]+e.cost){ //在新流下考虑最小费用
          d[v]=d[u]+e.cost;
          a[v]=min(a[u], e.cap-e.flow);
          p[v]=G[u][i];
          if(!inq[v]){ inq[v]=1; q.push(v);}
        }
      }
    }
    if(d[t]==inf)return false;
    flow+=a[t],cost+=a[t]*d[t];
    int u=t;
    while(u!=s){
      es[p[u]].flow+=a[t];
      es[p[u]^1].flow-=a[t];
      u=es[p[u]].u;
    }
    return true;
  }

  Type Mincost(int s,int t){
    Type flow=0, cost=0;
    while(spfa(s,t,flow,cost));
  // cout<<flow<<endl;
   if(flow!=2)
    return -1;
    return cost;
  }
}x;
int n,m;
int main()
{ int c,t,num;
  string a,b;
  scanf("%d",&t);
    while(t--){
          scanf("%d",&m);
          num=1;
          int kk=m*2;
          x.init(m*5);
          mp.clear();
          for(int i=0;i<m;i++){
             cin>>a>>b>>c;
             if(!mp[a]){
                mp[a]=num++;
                if(a=="Shanghai")
                    x.add_edge(mp[a],mp[a]+kk,2,0);
                else
                    x.add_edge(mp[a],mp[a]+kk,1,0);
             }
             if(!mp[b]){
                 if(b=="Shanghai")
                    x.add_edge(mp[b],mp[b]+kk,2,0);
                 else
                    x.add_edge(mp[b],mp[b]+kk,1,0);
             }
             x.add_edge(mp[a]+kk,mp[b],inf,c);
             x.add_edge(mp[b]+kk,mp[a],inf,c);
             //cout<<mp[a]<<"bq"<<mp[b]<<c<<endl;
          }
          x.add_edge(0,mp["Dalian"],1,0);
          x.add_edge(0,mp["Xian"],1,0);
          x.add_edge(mp["Shanghai"]+kk,kk*2+1,2,0);
          //cout<<"!!!"<<num<<endl;
          printf("%d\n",x.Mincost(0,kk*2+1));
     }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值