题目描述:

在一个有向图有n个顶点(编号从1到n),给一个起点s,问从起点出发,至少经过一条边,回到起点的最短距离。

输入:

输入包括多组,每组输入第一行包括三个整数n,m,s(1<=n<=500,0<=m<=10000,1<=s<=n),接下来有m行,每行包括三个整数a,b,c(1<=a,b<=n,1<=c<=1000),表示有一条a到b的边,长度为c。

输出:

对每组输入。输出最短距离,如果没有这个一条路径输出"help!"。

样例输入:
5 6 1
1 2 1
2 3 2
3 4 1
4 5 1
3 1 3
4 1 1
样例输出:
5

看网上的做法,都使用迪杰斯特拉算法做的,而我采用的是广度优先搜索的办法
但一开始的代码提交错误,如下
 #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <vector>
using namespace std; int n,m,s;
queue <int> que;
typedef pair<int, int> Edge;
vector<Edge> edge[];
int step[]; int main(int argc, char const *argv[])
{
while(scanf("%d %d %d",&n,&m,&s) != EOF) {
while(!que.empty()) {
que.pop();
}
for(int i = ; i < n; i++) {
edge[i].clear();
}
memset(step, , sizeof(step));
while(m--) {
int a, b, d;
scanf("%d %d %d",&a,&b,&d);
edge[a].push_back(Edge(b,d));
}
que.push(s);
while(!que.empty()) {
int p = que.front();que.pop();
int sizep = edge[p].size(); for(int i = ; i < sizep; i++) {
int tp = edge[p][i].first;
int ct = edge[p][i].second;
int pt = step[p] + ct;
if(step[tp] == || pt < step[tp]) {
step[tp] = pt;
que.push(tp);
}
}
}
if(step[s] == ) {
puts("help!");
}
else {
printf("%d\n",step[s]);
}
}
return ;
}

这段代码犯了两个错误,一是20行, clear不应该只到n,万一第二次输入的n比第一次的n小呢?

第二是题目中有 起点s到起点s的边,需要特殊处理,代码如下

 #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <vector>
using namespace std; int n,m,s;
queue <int> que;
typedef pair<int, int> Edge;
vector<Edge> edge[];
int step[]; int main(int argc, char const *argv[])
{
freopen("input.txt","r",stdin);
while(scanf("%d %d %d",&n,&m,&s) != EOF) {
while(!que.empty()) {
que.pop();
}
for(int i = ; i < ; i++) {
edge[i].clear();
}
memset(step, , sizeof(step));
while(m--) {
int a, b, d;
scanf("%d %d %d",&a,&b,&d);
edge[a].push_back(Edge(b,d));
}
que.push(s);
while(!que.empty()) {
int p = que.front();que.pop();
int sizep = edge[p].size(); for(int i = ; i < sizep; i++) {
int tp = edge[p][i].first;
int ct = edge[p][i].second;
int pt = step[p] + ct;
if(p == s) {
pt = ct;
}
if(step[tp] == || pt < step[tp]) {
step[tp] = pt;
que.push(tp);
}
}
}
if(step[s] == ) {
puts("help!");
}
else {
printf("%d\n",step[s]);
}
}
return ;
}

九度oj 题目1411:转圈的更多相关文章

  1. 九度OJ 题目1384:二维数组中的查找

    /********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...

  2. hdu 1284 关于钱币兑换的一系列问题 九度oj 题目1408:吃豆机器人

    钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  3. 九度oj题目&amp;吉大考研11年机试题全解

    九度oj题目(吉大考研11年机试题全解) 吉大考研机试2011年题目: 题目一(jobdu1105:字符串的反码).    http://ac.jobdu.com/problem.php?pid=11 ...

  4. 九度oj 题目1007:奥运排序问题

    九度oj 题目1007:奥运排序问题   恢复 题目描述: 按要求,给国家进行排名. 输入:                        有多组数据. 第一行给出国家数N,要求排名的国家数M,国家号 ...

  5. 九度oj 题目1087:约数的个数

    题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...

  6. 九度OJ题目1105:字符串的反码

    tips:scanf,cin输入字符串遇到空格就停止,所以想输入一行字符并保留最后的"\0"还是用gets()函数比较好,九度OJ真操蛋,true?没有这个关键字,还是用1吧,还是 ...

  7. 九度oj题目1009:二叉搜索树

    题目描述: 判断两序列是否为同一二叉搜索树序列 输入:                        开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束. 接 ...

  8. 九度oj题目1002:Grading

    //不是说C语言就是C++的子集么,为毛printf在九度OJ上不能通过编译,abs还不支持参数为整型的abs()重载 //C++比较正确的做法是#include<cmath.h>,cou ...

  9. 九度OJ题目1003:A+B

    while(cin>>str1>>str2)就行了,多简单,不得不吐槽,九度的OJ真奇葩 题目描述: 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号", ...

随机推荐

  1. sshd_config配置注释

    # $OpenBSD: sshd_config,v 1.80 2008/07/02 02:24:18 djm Exp $ # This is the sshd server system-wide c ...

  2. 如何修改Ruby的gem源(gem sources)

    Ruby环境下的gem sources地址默认是国外网络地址,所以在使用gem的过程中经常会出现找不到资源的Error.那么如何解决这种Error?方法很简单:要么就多次尝试执行gem命令,要么就修改 ...

  3. Python 多线程应用

    同步锁 import time import threading def subNum(): global num # print("ok") lock.acquire() # 加 ...

  4. TabControl重写,添加关闭按钮

    class userTabControl : TabControl { const int CLOSE_SIZE = 15; protected override void OnInvalidated ...

  5. 【主席树 启发式合并】bzoj3123: [Sdoi2013]森林

    小细节磕磕碰碰浪费了半个多小时的时间 Description Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M ...

  6. 【AC自动机】bzoj3172: [Tjoi2013]单词

    fail图上后缀和需要注意一下 Description 某人读论文,一篇论文是由许多单词组成.但他发现一个单词会在论文中出现很多次,现在想知道每个单词分别在论文中出现多少次. Input 第一个一个整 ...

  7. Linux下打包解压命令

    tar 压缩: tar -cvjpf etc.tar.bz2 /etc //-c为创建一个打包文件,相应的-f后面接创建的文件的名称,使用了.tar.bz2后缀,-j标志使用bzip2压缩,最后面为具 ...

  8. Python爬虫系列-Selenium+Chrome/PhantomJS爬取淘宝美食

    1.搜索关键字 利用Selenium驱动浏览器搜索关键字,得到查询后的商品列表 2.分析页码并翻页 得到商品页码数,模拟翻页,得到后续页面的商品列表 3.分析提取商品内容 利用PyQuery分析源码, ...

  9. python-numpy-pandas

    目录 numpy 模块 创建矩阵方法: 获取矩阵的行列数 切割矩阵 矩阵元素替换 矩阵的合并 通过函数创建矩阵 矩阵的运算 pandas模块 series (一维列表) DataFrame DataF ...

  10. redis列表,字典,管道,vue安装,创建项目

    redis mysql,redis,mogondb 1.mysql,oracle:关系型数据库,有表概念 2.redis,mongodb/nosql:非关系型数据库 没有表概念 mongodb存储在硬 ...