D. Lunar New Year and a Wander bfs+优先队列

题意

给出一个图,从1点开始走,每个点至少要经过一次(可以很多次),每次经过一个没有走过的点就把他加到走过点序列中,问最小字典序的序列是多少

思路

起始就是从每次可达的点的选取最小的那个走,拓展可达的点,然后重复直到走完了全部为止,直接用个bfs+优先队列即可

#include<bits/stdc++.h>
#include<stdlib.h>
using namespace std;
const int maxn = 3e5+5;
typedef long long ll;
#define F first
#define S second
#define pb push_back
#define pii pair<int ,int >
int a[maxn],c[maxn];
vector<int>G[maxn];
int vis[maxn]; bool cmp(int x,int y){
char num1[10],num2[10];
sprintf(num1,"%d",x);
sprintf(num2,"%d",y);
int len1=strlen(num1),len2=strlen(num2);
for(int i=0;i<min(len1,len2);i++){
if(num1[i]>num2[i]){
return 0;
}
else if(num1[i]<num2[i])return 1;
}
return len1<=len2; }
priority_queue<int,vector<int>,greater<int> >q;
void bfs(){
q.push(1);
vis[1]=1;
while(!q.empty()){
int tmp=q.top();
q.pop();
cout<<tmp<<" ";
for(int i=0;i<G[tmp].size();i++)
{
int y=G[tmp][i];
if(!vis[y]){q.push(y);vis[y]=1;}
}
}
}
int main(){
int n,m,t,d;
scanf("%d%d",&n,&m);
int x,y;
for(int i=0;i<m;i++){
scanf("%d%d",&x,&y);
G[x].pb(y);
G[y].pb(x);
} bfs(); return 0;
}

D. Lunar New Year and a Wander bfs+优先队列的更多相关文章

  1. POJ 1724 ROADS(BFS+优先队列)

    题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...

  2. hdu 1242 找到朋友最短的时间 (BFS+优先队列)

    找到朋友的最短时间 Sample Input7 8#.#####. //#不能走 a起点 x守卫 r朋友#.a#..r. //r可能不止一个#..#x.....#..#.##...##...#.... ...

  3. HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)

    题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...

  4. hdu1839(二分+优先队列,bfs+优先队列与spfa的区别)

    题意:有n个点,标号为点1到点n,每条路有两个属性,一个是经过经过这条路要的时间,一个是这条可以承受的容量.现在给出n个点,m条边,时间t:需要求在时间t的范围内,从点1到点n可以承受的最大容量... ...

  5. BFS+优先队列+状态压缩DP+TSP

    http://acm.hdu.edu.cn/showproblem.php?pid=4568 Hunter Time Limit: 2000/1000 MS (Java/Others)    Memo ...

  6. POJ - 2312 Battle City BFS+优先队列

    Battle City Many of us had played the game "Battle city" in our childhood, and some people ...

  7. HDU 1242 -Rescue (双向BFS)&amp;&amp;( BFS+优先队列)

    题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...

  8. hdu 2102 A计划 具体题解 (BFS+优先队列)

    题目链接:pid=2102">http://acm.hdu.edu.cn/showproblem.php?pid=2102 这道题属于BFS+优先队列 開始看到四分之中的一个的AC率感 ...

  9. Codeforce-1106-D. Lunar New Year and a Wander(DFS遍历+vector存图+set)

    Lunar New Year is approaching, and Bob decides to take a wander in a nearby park. The park can be re ...

随机推荐

  1. VSCode部署JAVA项目出现The type java.lang.Object cannot be resolved

    如题,出现的原因是这样的:我将mac系统上的eclipse项目复制到了ubuntu环境下,通过vscode的远程功能连接ubuntu. 然后项目上就出现了各种报错,显示The type java.la ...

  2. SQL Server database – Error 3743

    Database mirroring must be removed before you drop SQL Server database – Error 3743 If you try to dr ...

  3. OCM 12c | OCM 12c Update | OCM 11g (Retiring Dec 31, 2019) | OCM 11g考试延期至2020.04.30

     OCM 全球考试安排时间表 View A Worldwide OCM Schedule Oracle Database 12c Certified Master Exam (OCM) OCM 12c ...

  4. HDU1548 奇怪的电梯(bfs求最少)

    There is a strange lift.The lift can stop can at every floor as you want, and there is a number Ki(0 ...

  5. weflow的坑

    开发工具weflow的坑 使用less,css预处理器.如果less文件有问题项目不能正常打开需要排除错误后才能正常打开.今天遇到个问题background:rgba(255,255,255 .35) ...

  6. Longest Ordered Subsequence POJ - 2533 dp 最长上升/不下降 子序列

    #include<iostream> using namespace std ; ; int f[N]; int a[N]; int n; int main() { cin>> ...

  7. 22.01.Cluster

      1. 클러스터링 iris 데이터셋 확인¶ In [2]: from sklearn import cluster from sklearn import datasets iris = dat ...

  8. 4.Docker 操作容器

    启动容器 所需要的命令主要为 docker run.例如,下面的命令输出一个 “Hello World”,之后终止容器. docker run ubuntu:16.04 /bin/echo 'Hell ...

  9. Wannafly Camp 2020 Day 2I 堡堡的宝藏 - 费用流

    感谢这道题告诉我KM求的是 完备 最大权匹配 :( #include <bits/stdc++.h> using namespace std; #define reset(x) memse ...

  10. linux - mysql 异常:ERROR 1820 (HY000): You must SET PASSWORD before executing this statement

    问题描述 ERROR 1820 (HY000): You must SET PASSWORD before executing this statement 备注:新安装完数据库后,在 xshell ...