九度oj 题目1411:转圈
- 题目描述:
-
在一个有向图有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:转圈的更多相关文章
- 九度OJ 题目1384:二维数组中的查找
/********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...
- hdu 1284 关于钱币兑换的一系列问题 九度oj 题目1408:吃豆机器人
钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- 九度oj题目&吉大考研11年机试题全解
九度oj题目(吉大考研11年机试题全解) 吉大考研机试2011年题目: 题目一(jobdu1105:字符串的反码). http://ac.jobdu.com/problem.php?pid=11 ...
- 九度oj 题目1007:奥运排序问题
九度oj 题目1007:奥运排序问题 恢复 题目描述: 按要求,给国家进行排名. 输入: 有多组数据. 第一行给出国家数N,要求排名的国家数M,国家号 ...
- 九度oj 题目1087:约数的个数
题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...
- 九度OJ题目1105:字符串的反码
tips:scanf,cin输入字符串遇到空格就停止,所以想输入一行字符并保留最后的"\0"还是用gets()函数比较好,九度OJ真操蛋,true?没有这个关键字,还是用1吧,还是 ...
- 九度oj题目1009:二叉搜索树
题目描述: 判断两序列是否为同一二叉搜索树序列 输入: 开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束. 接 ...
- 九度oj题目1002:Grading
//不是说C语言就是C++的子集么,为毛printf在九度OJ上不能通过编译,abs还不支持参数为整型的abs()重载 //C++比较正确的做法是#include<cmath.h>,cou ...
- 九度OJ题目1003:A+B
while(cin>>str1>>str2)就行了,多简单,不得不吐槽,九度的OJ真奇葩 题目描述: 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号", ...
随机推荐
- Berkeley DB (VC6.0 编译环境配置)
操作系统:winxp VC环境:VC6.0 必需文件:Berkeley DB安装文件(db-.msi) 下载地址:http://www.oracle.com/technology/software/p ...
- iBatis自动生成工具Abator
https://blog.csdn.net/k_scott/article/details/8281837 ###首先创建数据库表,然后根据数据库表,生成相应的实体.及其配置文件 https://ww ...
- 使用max函数计算EXCEL个税公式
1.Max()函数是求括号内的数的最大值.2.其中,第一和第二个大括号{}内的数,相信作为财务的应该很清楚,就是个人所得税的缴税比例,以及速算个人应缴所得税的相关数据.3.在EXCEL中,使用{}表示 ...
- Boo who-freecodecamp算法题目
Boo who 1.要求 检查一个值是否是基本布尔类型,并返回 true 或 false. 基本布尔类型即 true 和 false 2.思路 利用switch语句判断输入的数据是true/false ...
- 【费用流】bzoj1834: [ZJOI2010]network 网络扩容
还是稍微记一下这个拆点模型吧 Description 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用. 求: 1.在不扩容的情况下,1到N的最大流: ...
- python入门:输出1-10的所有数(自写)
#!/usr/bin/env python # -*- coding:utf-8 -*- #输出1-10的所有数(自写) """ 导入time库,给kaishi赋值为数字 ...
- 【Kafka】搭建和测试等问题
1.安装启动kafka #跳转到下载目录cd /opt/setup # 下载安装包 wget http://mirror.bit.edu.cn/apache/kafka/0.10.2.0/kafka_ ...
- windows下配置Nginx支持php
编辑配置文件nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; ...
- Ubuntu安装sogou拼音输入法
1.更新系统:sudo apt-get update 2.更新相关依赖 sudo apt-get install fcitx -f 2.安装fcitx:sudo apt-get install fci ...
- linux下GPIO的用户层操作(sysfs)
linux的GPIO通过sysfs为用户提供服务,下面是linux kernel里的说明文档,学习一下. GPIO Sysfs Interface for Userspace ============ ...