题目描述:

在一个有向图有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. 小技巧:unicode RLO

    unicode 控制字符 RLO 可以将位于其后的文字翻转. 于是可以被病毒利用. 如图 重命名文件,在gpj前插入unicode RLO,之后若不小心,可能会被欺骗,误以为是jpg文件. 如果修改程 ...

  2. js判断是否为app

    var ua = navigator.userAgent; var isapp = ua.match("lenovomallapp") == null ? 0 : 1;

  3. npm 安装插件失败

    由于npm的很多安装包的下载源来自国外网站,所以比较缓慢甚至访问失败. 再此可以用淘宝的镜像文件来安装插件.方法其实很简单:

  4. Dojo中的选择器

    dom.byId(以前的dojo.byId):等同于js中的document.getElementById. http://www.cnblogs.com/tiandi/archive/2013/11 ...

  5. orcle定时备份

    orcle定时备份 (1)写个.bat文件 例如: exp test/test@ORCL file=f:\back\test%date:~,%%date:~,%%date:~,%.dmp (2)开始, ...

  6. sencha touch 入门学习资料大全

    现在sencha touch已经更新到2.3.1版本了 重新整理一下资料 官方网站:http://www.sencha.com/products/touch/ 在线文档:http://docs.sen ...

  7. lua 使用递归查找键值

    function cc.exports.findValueByTbl(tbl,key)--递归方法,用于查找tbl中对应的键值 for k,v in pairs(tbl) do if k == key ...

  8. pre-commit钩子,代码质量检查

    目前基本使用三款js代码质量检查工具: jslint, jshint, eslint.许多IDE里面也有对应的检查插件,在每次ctrl + s 保存文件的时候,检查当前文件是否符合规范,保证代码质量. ...

  9. 【netbeans】【ubuntu】ubuntu netbeans 抗锯齿化修复

    每一个在ubuntu下用netbeans的,都会对它的字体怎么会显示的那么难看表示很不理解.我就是因此几乎没有用netbeans的.   不过今天终于解决问题了,虽然没有eclipse显示的那么漂亮, ...

  10. CodeForce:16C-Monitor

    传送门:http://codeforces.com/problemset/problem/16/C Monitor time limit per test0.5 second memory limit ...