首页 > 其他分享 >G. Rudolf and Subway

G. Rudolf and Subway

时间:2024-03-15 20:56:53浏览次数:27  
标签:emplace int back next Subway now Rudolf dis

原题链接

题解

太巧妙了!!
原题等效于该分层图,然后广搜
本题中我用了另一种方法建边,因为清空太麻烦了

code

#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        int n,m;
        cin>>n>>m;
        map<int,vector<int> > G;
        map<int,int> dis;
        for(int i=1;i<=m;i++)
        {
            int x,y,w;
            cin>>x>>y>>w;
            w+=n;
            G[x].emplace_back(w);
            G[y].emplace_back(w);
            G[w].emplace_back(x);
            G[w].emplace_back(y);
        }

        int st,ed;
        cin>>st>>ed;
        queue<int> q;
        q.emplace(st);
        dis[st]=1;
        while(q.size())
        {
            int now=q.front();
            q.pop();
            //printf("dis[%d]=%d\n",now,dis[now]);
            if(now==ed)break;
            for(auto next:G[now])
            {
                if(!dis[next])
                {
                    dis[next]=dis[now]+1;
                    q.emplace(next);
                }
            }
        }
        cout<<dis[ed]/2<<endl;
    }
    return 0;
}

标签:emplace,int,back,next,Subway,now,Rudolf,dis
From: https://www.cnblogs.com/pure4knowledge/p/18076215

相关文章

  • F. Rudolf and Imbalance
    原题链接题解最大值最小\(\to\)二分可行性判断:二分间断值\(len\\to\)如果原序列\(a_i-a_{i-1}>len\)\(\to\)双指针判断有没有\(b+f\)使得\(a_i-len<=b+f<=a_{i-1}+len\)由于只能使用一次,所以若使用两次也算错code#include<bits/stdc++.h>usingnamespacestd;......
  • POJ1635subway tree system
    在扫描过程中一旦扫描到一个子串01数量相等了,这个时候肯定是已经递归回到根节点了,因为从根节点下去的一步操作给了一个0,而这个0一定要从这条边回到根节点才能产生一个1与其匹配(这个1不可能来自其他边的回溯,因为其他边的回溯的前提就是之前从这条边下去了,就会产生一个0,,这个0就要......
  • CF1846E2 Rudolf and Snowflakes (hard version) 题解
    题意:\(T\)\((\)\(1\)\(\le\)\(T\)\(\le\)\(10^4\)\()\)组询问:是否存在一个满\(k\)(\(k\)\(\ge\)\(2\)\()\)叉树节点数恰好为\(n\)\((\)\(1\)\(\le\)\(n\)\(\le\)\(10^{18}\)\()\),且深度\(depth\)至少为\(2\)。思路:满$k$......
  • CF131D Subway 题解
    题目传送门前置知识强连通分量|最短路解法考虑用Tarjan进行缩点,然后跑最短路。缩点:本题的缩点有些特殊,基于有向图缩点修改而得,因为是无向图,所以在Tarjan过程中要额外记录一下从何处转移过来,防止在同一处一直循环。基环树上找环还有其他方法,这里仅讲解使用Tarjan求......
  • 1131 Subway Map
    题目:Inthebigcities,thesubwaysystemsalwayslooksocomplextothevisitors.Togiveyousomesense,thefollowingfigureshowsthemapofBeijingsubway.Nowyouaresupposedtohelppeoplewithyourcomputerskills!Giventhestartingpositionofy......
  • CF1060E Sergey and Subway
    题目大意给定一棵树,每两个有边直接相连的点之间距离为\(1\)。现在我们要给所有原来距离为\(2\)的城市之间修一条长度为\(1\)的道路。记\(\operatorname{dis}(a,b)\)表示\(a,b\)之间的最短距离,求\[\sum_{i=1}^n\sum^{n}_{j=i+1}\operatorname{dis}(i,j)\]思路考虑修......
  • CF1060E Sergey and Subway 题解
    题面由题意可知,在原图中经过边数为\(2\)的一对点,在新图中经过边数为\(1\)。所以每对点在新图中的距离为:\[\begin{aligned}\lceil\frac{dis(i,j)}{2}\rceil=\frac{dis(i,j)+dis(i,j)\;mod\;2}{2}\end{aligned}\]那么我们只需在原图上求出任意两点距离之和并加上\(dis......
  • CF131D Subway
    题目链接:洛谷CodeforcesSolutionTarjan板题。很明显可以用Tarjan找到这一个环,由于这是一个无向图,所以需要多记录一个当前节点的父亲,防止其反复横跳。然后缩完点以......