团体程序设计天梯赛(L3-014 周游世界 (30 分))

题目:

思路分析:

最开始以为要建分层图可是一看数据范围

dfs暴力也可以呀

就是最短路+路径存储的题目 当然dijsktra也行!

代码实现:

/*
*@Author:   GuoJinlong
*@Language: C++
*/
//#include <bits/stdc++.h>
/*
 *                                                     __----~~~~~~~~~~~------___
 *                                    .  .   ~~//====......          __--~ ~~
 *                    -.            \_|//     |||\\  ~~~~~~::::... /~
 *                 ___-==_       _-~o~  \/    |||  \\            _/~~-
 *         __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
 *     _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
 *   .~       .~       |   \\ -_    /  /-   /   ||      \   /
 *  /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
 *  |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
 *           '         ~-|      /|    |-~\~~       __--~~
 *                       |-~~-_/ |    |   ~\_   _-~            /\
 *                            /  \     \__   \/~                \__
 *                        _--~ _/ | .-~~____--~-/                  ~~==.
 *                       ((->/~   '.|||' -_|    ~~-/ ,              . _||
 *                                  -_     ~\      ~~---l__i__i__i--~~_/
 *                                  _-~-__   ~)  \--______________--~~
 *                                //.-~~~-~_--~- |-------~~~~~~~~
 *                                       //.-~~~--\
 *                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 *                               神兽保佑            永无BUG
 */

const int MAX=10010;
vector<int>path,tmp;
vector<int>v[MAX];
int line[MAX][MAX];
int n,m,k;
int count(vector<int>a){
    int pre=0;
    int cnt=-1;
    
    for(int i=1;i<a.size();i++){
        if(line[a[i-1]][a[i]]!=pre)
            cnt++;
        pre=line[a[i-1]][a[i]];
    }
    return cnt;
}
int vis[MAX];
int m1,m2;
void dfs(int st,int en,int cnt){
    if(st==en){
        if(cnt<m1||(m2>count(tmp)&&m1==cnt)){
            path=tmp;
            m1=cnt;
            m2=count(tmp);
        }
        return;
    }
    for(int i=0;i<v[st].size();i++){
        int u=v[st][i];
        if(vis[u]) continue;
        vis[u]=1;
        tmp.push_back(u);
        dfs(u,en,cnt+1);
        vis[u]=0;
        tmp.pop_back();
    }
}
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>k;
        
        int x;
        int pre;
        cin>>pre;
        for(int j=2;j<=k;j++){
            cin>>x;
            v[x].push_back(pre);
            v[pre].push_back(x);
            line[x][pre]=i;
            line[pre][x]=i;
            pre=x;
        }
    }
    cin>>m;
    
    while (m--) {
        int x,y;
        cin>>x>>y;
        tmp.clear();
        tmp.push_back(x);
        vis[x]=1;
        m1=INF;
        m2=INF;
        dfs(x,y,0);
//        cout<<m1<<" "<<m2<<endl;
        vis[x]=0;
        if(m1>=INF)  {cout<<"Sorry, no line is available.\n";continue;}
        cout<<m1<<endl;
        int p1=x;
        int p2=0;
        for(int i=1;i<path.size();i++){
            if(line[path[i-1]][path[i]]!=p2){
                if(p2!=0){
                    printf("Go by the line of company #%d from %04d to %04d.\n", p2, p1, path[i-1]);
                }
                p1=path[i-1];
                p2=line[path[i-1]][path[i]];
            }
        }
        printf("Go by the line of company #%d from %04d to %04d.\n", p2, p1, y);
        
    }
}

猜你喜欢

转载自blog.csdn.net/m0_57006708/article/details/121260275