首页 > 其他分享 >getline与stringstream的用法

getline与stringstream的用法

时间:2022-10-28 16:46:45浏览次数:47  
标签:cnt dist int stop 用法 ++ stringstream getline

https://www.acwing.com/problem/content/922/

#include <bits/stdc++.h>
using namespace std;

const int N = 505;

int n, m;
int dist[N], stop[N], q[N];
bool g[N][N];

void bfs() {
    memset(dist, 0x3f, sizeof dist);
    dist[1] = 0;
    int hh = 0, tt = 0;
    q[0] = 1;

    while (hh <= tt) {
        auto t = q[hh ++];

        for (int i = 1; i <= n; i ++)
            if (g[t][i] && dist[i] > dist[t] + 1) {
                dist[i] = dist[t] + 1;
                q[++ tt] = i;
            }
    }
}

int main() {
    cin >> m >> n;
    
    string str;
    getline(cin, str);
    while (m --) {
        getline(cin, str);
        stringstream ssin(str);
        int cnt = 0, p;
        while (ssin >> p) stop[cnt ++] = p;

        for (int i = 0; i < cnt; i ++)
            for (int j = i + 1; j < cnt; j ++)
                g[stop[i]][stop[j]] = true;
    }

    bfs();

    if (dist[n] == 0x3f3f3f3f) cout << "NO" << endl;
    else cout << max(dist[n] - 1, 0) << endl;
    return 0;
}

 

标签:cnt,dist,int,stop,用法,++,stringstream,getline
From: https://www.cnblogs.com/Leocsse/p/16836571.html

相关文章

  • Cesium的HeadingPitchRange 用法
    这个有别于headingpitchroll,headingpitchroll是用在orientation属性上的(比如相机的setView,flyTo,以及entities.add中)HeadingPitchRange一般用在模型加载之后的定位上,比如v......
  • 看了我的 mybatis-plus 用法,全公司同事开始悄悄模仿了。。
    本文主要介绍mybatis-plus这款插件,针对springboot用户。包括引入,配置,使用,以及扩展等常用的方面做一个汇总整理,尽量包含大家常用的场景内容。作者:我犟不过你原文......
  • java-guava 布隆筛选器用法及比较
    java-guava布隆筛选器用法及比较布隆筛选器使用场景:   一般用于一个字符串是否存的预测,如爬虫是否抓取过这个URL等。优点:   对于特大的集合来说,检索快、占用内......
  • es6 reduce()的一些用法
    //计算数组中最大值constarr=[1,2,3,4,5,6,7,8,9]letmax=arr.reduce((max,age)=>{returnmax>age?max:age},0)<!--console.log(max)-->//数......
  • <四>1:全面掌握Const的用法
    const怎么理解?const修饰的变量不能够在作为左值!!初始化完成后,值不能被修改!!C和C++中const的区别?在C程序中test.cconstinta;只定义,不做初始化(C中允许),如......
  • 动态数组vector的相关用法
    1)头文件#include<vector>2)创建一维vector对象,vector<int>vec;3)尾部插入数字:vec.push_back(a);4)尾部元素弹出:vec.pop_back();相当于删除尾部元素。5)使用下标访问......
  • Spark SQL概述、函数用法
    SparkSQL  底层还是基于RDD的,常用的语言DSL底层架构    在idea中的操作引入pom依赖<dependency><groupId>org.apache.spark</gr......
  • ansible-playbook 用法
     catinstall_zabbix_3.yaml----name:#名称hosts:new#hosts为文件名,new为hosts文件里得[new]tasks:#任务-name:shell:|rpm-ivhhtt......
  • Python之JSON用法解析
    前景Python编写HDFS服务安装的过程中,需要将构建好的JSON对象输出到文件,采用那种方式更便捷方案1open函数defwriteExecCmdCheckActionsFile(self,out_res,che......
  • mysql group by having用法
    mysql中groupby里面的问题GROUPBYdept,name的意思,就是先按dqpt分组,如果出现dept相同的,再按name分组,也就是说除非出现dept和name都相同的记录才会合为一组,否则的话是不会合......