码迷,mamicode.com
首页 > 其他好文 > 详细

1131 Subway Map

时间:2020-03-16 18:59:04      阅读:53      评论:0      收藏:0      [点我收藏+]

标签:tmp   tps   climits   final   amp   target   cto   nbsp   limits   

link

技术图片

 

技术图片

 

 技术图片

 

 

 

#include <iostream>
#include <cstring>
#include <vector>
#include <unordered_map>
#include <climits>
# define LL long long
using namespace std;

vector<vector<int>> adj(10000);
unordered_map<int,unordered_map<int,int>> line;
vector<int> finalpath;
int transfers;
int mincnt;
int visited[10000];

int countTransfers(vector<int>& path){
    int preline=0;
    int res=0;
    for(int i=0;i<path.size()-1;i++){
        int u=path[i];
        int v=path[i+1];
        int curline=line[u][v];
        if(preline==0){
            preline=curline;
            continue;
        }
        if(curline!=preline){
            preline=curline;
            res++;
        }
    }
    return res;
}

void dfs(int u, int v, int cnt, vector<int>& path){
    if(visited[u]==1) return;
    if(cnt>mincnt) return;
    if(u==v){
        path.push_back(v);
        if(cnt<mincnt){
            mincnt=cnt;
            finalpath=path;
            transfers=countTransfers(path);
        }else if(cnt==mincnt){
            int tmp=countTransfers(path);
            if(tmp<transfers){
                transfers=tmp;
                finalpath=path;
            }
        }
        path.pop_back();
        return;
    }

    visited[u]=1;
    path.push_back(u);
    for(int next:adj[u]){
        dfs(next,v,cnt+1,path);
    }
    visited[u]=0;
    path.pop_back();
}

int main(){
    int N;
    cin>>N;
    for(int i=1;i<=N;i++){
        int cnt;
        cin>>cnt;
        int pre;
        cin>>pre;
        for(int j=1;j<cnt;j++){
            int cur;
            cin>>cur;
            adj[pre].push_back(cur);
            adj[cur].push_back(pre);
            line[cur][pre]=i;
            line[pre][cur]=i;
            pre=cur;
        }
    }

    int K;
    cin>>K;
    for(int i=0;i<K;i++){
        memset(visited,0,sizeof(visited));
        int u,v;
        cin>>u>>v;
        vector<int> path;
        mincnt=INT_MAX;
        dfs(u,v,0,path);
        printf("%d\n", finalpath.size()-1);
        int preline=0;
        int start=u;
        for(int j=0;j<finalpath.size()-1;j++){
            int a=finalpath[j];
            int b=finalpath[j+1];
            int curline=line[a][b];
            if(preline==0){
                preline=curline;
            }else{
                if(preline!=curline){
                    printf("Take Line#%d from %04d to %04d.\n", preline,start,a);
                    start=a;
                    preline=curline;
                }
            }
            if(b==v){
                printf("Take Line#%d from %04d to %04d.\n", curline,start,v);
            }
        }
    }
    return 0;
}

 

1131 Subway Map

标签:tmp   tps   climits   final   amp   target   cto   nbsp   limits   

原文地址:https://www.cnblogs.com/FEIIEF/p/12505763.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!