zoukankan      html  css  js  c++  java
  • 2017 乌鲁木齐赛区网络赛 J 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~(mle 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 -11 if it does not exist.

    题意抽象一下即为要求从A经过B到达C的最短路,并且要求路径上的点不可重复经过。

    拆点后点内限流,建立源点S指向B容量为2的一条边,A,C分别建立一条指向汇点T容量为1的边。然后依题意建图即可。

    跑一遍费用流便得到答案。

      1 #include<bits/stdc++.h>
      2 #define rep(i,a,b) for(int i=a;i<=b;i++)
      3 using namespace std;
      4 const int MAXN=101000;
      5 const int maxn=20000;
      6 const int INF=~0U>>1;
      7 int maxflow=0,cost=0;
      8 int pre[maxn],vis[maxn],dis[maxn];
      9 int S,T,S1;
     10 int n,m;
     11 int tot=0;
     12 int pointer[maxn];
     13 map<string,int> mp;
     14 int shanghai;
     15 struct Edge
     16 {
     17     int to,next,cap,f,w;
     18     Edge() {};
     19     Edge(int b,int c,int nxt,int flow,int weight) {to=b,cap=c,next=nxt,f=flow,w=weight;}
     20 }edge[MAXN];
     21 inline void addedge(int a,int b,int c,int w1)
     22 {
     23     edge[tot]=Edge(b,c,pointer[a],0,w1);
     24     pointer[a]=tot++;
     25     edge[tot]=Edge(a,0,pointer[b],0,-w1);
     26     pointer[b]=tot++;
     27 }
     28 bool spfa(int s,int t)
     29 {
     30     queue<int > q;
     31     rep(i,S,T)
     32     {
     33         dis[i]=INF;
     34         vis[i]=false;
     35         pre[i]=-1;
     36     }
     37     dis[S]=0;
     38     vis[S]=1;
     39     q.push(S);
     40     while(!q.empty())
     41     {
     42         int u=q.front();q.pop();
     43         vis[u]=0;
     44         for(int j=pointer[u];j!=-1;j=edge[j].next)
     45         {
     46             int v=edge[j].to;
     47       //      if(u==92) printf("u=92 v=%d
    ",v);
     48             if(edge[j].cap-edge[j].f>0&&dis[v]>dis[u]+edge[j].w)
     49             {
     50                 dis[v]=dis[u]+edge[j].w;
     51                 pre[v]=j;
     52                 if(!vis[v])
     53                 {
     54                     vis[v]=1;q.push(v);
     55                 }
     56             }
     57         }
     58     }
     59     if(pre[T]==-1) return false;
     60     else return true;
     61 }
     62 int mcmf()
     63 {
     64     int flow=0;
     65     cost=0;
     66     while(spfa(S,T))
     67     {
     68         int mi=INF;
     69         for(int i=pre[T];i!=-1;i=pre[edge[i^1].to])
     70         {
     71             mi=min(mi,edge[i].cap-edge[i].f);
     72         }
     73         for(int i=pre[T];i!=-1;i=pre[edge[i^1].to])
     74         {
     75             edge[i].f+=mi;
     76             edge[i^1].f-=mi;
     77            // printf("edge[%d].w=%d edge[i].to=%d
    ",i,edge[i].w,edge[i].to);
     78             cost+=edge[i].w*mi;
     79         }
     80         flow+=mi;
     81     }
     82     return flow;
     83 }
     84 void Input()
     85 {
     86     mp.clear();
     87     scanf("%d",&m);
     88     string a;
     89     string b;
     90     int len;
     91     int cnt=1;
     92     memset(pointer,-1,sizeof(pointer));
     93     tot=0;
     94     rep(i,1,m)
     95     {
     96         cin>>a>>b>>len;
     97         if(mp[a]==0)
     98         {
     99             mp[a]=cnt;
    100             addedge(cnt,cnt+1,1,0);
    101             cnt+=2;
    102 
    103         }
    104         if(mp[b]==0)
    105         {
    106             mp[b]=cnt;
    107             addedge(cnt,cnt+1,1,0);
    108             cnt+=2;
    109         }
    110         addedge(mp[a]+1,mp[b],INF,len);
    111         addedge(mp[b]+1,mp[a],INF,len);
    112     }
    113     S=0;T=cnt;
    114     addedge(mp["Shanghai"],mp["Shanghai"]+1,1,0);
    115     addedge(S,mp["Shanghai"],2,0);
    116     addedge(mp["Xian"]+1,T,1,0);
    117     addedge(mp["Dalian"]+1,T,1,0);
    118 }
    119 int main()
    120 {
    121     freopen("in.txt","r",stdin);
    122     int T;
    123     scanf("%d",&T);
    124     rep(t1,1,T)
    125     {
    126         Input();
    127         maxflow=mcmf();
    128         if(maxflow==2) printf("%d
    ",cost);
    129         else printf("-1
    ");
    130     }
    131     return 0;
    132 }
  • 相关阅读:
    mysql性能优化学习笔记(1)优化目的、方向及数据库准备
    php笔试算法题:顺时针打印矩阵坐标蛇形算法
    离职了,一切从头开始
    mysql性能优化学习笔记(2)如何发现有问题的sql
    WTL的使用
    获取程序当前目录的绝对路径
    [读书笔记]《C#本质论》读书笔记
    [.NET]ConcurrentDictionary 线程安全的集合类
    RakNet中文手册
    Python SCons
  • 原文地址:https://www.cnblogs.com/zhixingr/p/7612369.html
Copyright © 2011-2022 走看看