题目描述:

在一个有向图有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. C基础的练习集及测试答案(1-15)

    练习题:注:标有(课堂)字样的为课上练习,其他为课下练习基础题(50题)1.(课堂)编写程序,输出“XXX欢迎来到动物园!”(XXX是自己的名字). //1.(课堂)编写程序,输出“XXX欢迎来到动物 ...

  2. CodeForces 66C Petya and File System (实现)

    模拟题,map搞一搞.要想清楚一个结点应该是要通过一个字符串找到下一个结点,题目保证所以文件夹非空,所以只要判断一个结点是不是叶子结点就可以判断它是不是文件,用了点c11的特性. #include&l ...

  3. Connectivity

    6492: Connectivity 时间限制: 1 Sec  内存限制: 128 MB提交: 118  解决: 28[提交][状态][讨论版][命题人:admin] 题目描述 There are N ...

  4. Html5的等学习

    看了w3c感觉是说明文档,没有详细的说明,然后就去看其他的 html5其实就是在html的基础上做了一些改变,感觉html5的推广还是需要时间的,因为习惯问题,虽然html5有很多很方便的标签如art ...

  5. Servlet的引入(即加入Servlet)

    今天讲的Servlet是根据上一章节<创建一个学生信息表,与页面分离>而结合. 一.看图分析 此模式有问题: 1.jsp需要呼叫javabean StudentService stuSer ...

  6. C/C++ 数组与指针

    #include <iostream>using namespace std;int main(){ char *a[]={"ab","ccs",& ...

  7. jQuery plugin : bgStretcher 背景图片切换效果插件

    转自:http://blog.dvxj.com/pandola/jQuery_bgStretcher.html bgStretcher 2011 (Background Stretcher)是一个jQ ...

  8. Java 编辑html模板并生成pdf

    1.工具类 import com.hujiang.project.zhgd.Util; import com.itextpdf.text.BaseColor; import com.itextpdf. ...

  9. 五一4天就背这些Python面试题了,Python面试题No12

    第1题: Python 中的 os 模块常见方法? os 属于 python内置模块,所以细节在官网有详细的说明,本道面试题考察的是基础能力了,所以把你知道的都告诉面试官吧 官网地址 https:// ...

  10. 命令行执行Qt程序

    原文网址 //helloworld.cpp #include <QApplication> #include <QPushButton> int main(int argc,c ...