题意::从起点到终点的所有的最短路中,找出离终点有X个路口的城市一共有几个

开始我用最短路+DFS从起点开始搜,超时了

换了一种方法,从终点开始搜,AC

#include<stdio.h>

int N;
const int MAX=1e9;
int use[];
int dis[];
int map[][];
bool hash[];
int tempget[];
int maxDis;
int allPoint; int start,end,shortNum; void dijk()
{
int i,j,min,rj,from;
dis[start]=;
for(i=;i<=N;i++){
min=MAX;
for(j=;j<=N;j++){
if(use[j]==)continue;
if(min>dis[j]){
min=dis[j];
rj=j;
}
}
from=rj;
for(j=;j<=N;j++){
if(use[j]==)continue;
if(dis[j]>map[from][j]+dis[from])
dis[j]=map[from][j]+dis[from];
}
use[rj]=;
}
} void dfs(int from,int leftStep,int lenth) //搜的时候从师兄所在的点开始搜
{
int i;
if(leftStep==)return ; for(i=;i<=N;i++){
if(hash[i]==)continue;
if((dis[i]+map[from][i]+lenth)!=dis[end])continue; //这样保证了从终点搜出的点都在最短路上
if(tempget[i]==){
allPoint++;tempget[i]=;
}
hash[i]=;
dfs(i,leftStep-,lenth+map[from][i]);
hash[i]=;
}
} int main()
{
int m,i,j,k;
while(scanf("%d%d",&N,&m)!=EOF){
int ll,rr,v; start=N;
end=;
allPoint=;
scanf("%d",&maxDis); for(i=;i<=N;i++){
for(j=;j<=N;j++){
map[i][j]=MAX;
hash[i]=;
}
dis[i]=MAX;
use[i]=;
tempget[i]=;
} for(i=;i<=m;i++){
scanf("%d%d%d",&ll,&rr,&v);
if(map[ll][rr]>v){
map[ll][rr]=map[rr][ll]=v;
}
} dijk();
tempget[]=end;
allPoint++;
dfs(end,maxDis,); printf("%d\n",allPoint);
} return ;
}

题目1539:师弟 ——最短路+DFS的更多相关文章

  1. PAT-1111 Online Map (30分) 最短路+dfs

    明天就要考PAT,为了应付期末已经好久没有刷题了啊啊啊啊,今天开了一道最短路,状态不是很好 1.没有读清题目要求,或者说没有读完题目,明天一定要注意 2.vis初始化的时候从1初始化到n,应该从0开始 ...

  2. HDU_1142(最短路 + dfs)

    Jimmy experiences a lot of stress at work these days, especially since his accident made working dif ...

  3. HDU 1142 A Walk Through the Forest(最短路+dfs搜索)

    A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  4. cf1051F. The Shortest Statement(最短路/dfs树)

    You are given a weighed undirected connected graph, consisting of nn vertices and mm edges. You shou ...

  5. Let‘s play computer game(最短路 + dfs找出所有确定长度的最短路)

    Let's play computer game Description xxxxxxxxx在疫情期间迷上了一款游戏,这个游戏一共有nnn个地点(编号为1--n1--n1--n),他每次从一个地点移动 ...

  6. 借助leetcode题目来了解BFS和DFS

    广度优先和深度优先搜索 前言 看着这两个搜索的前提的是读者具备图这一数据结构的基本知识,这些可以直接百度一波就了解了.图也像树一样,遍历具有很多的学问在里面,下面我将借用leetcode的题目讲解一下 ...

  7. 449. Serialize and Deserialize BST——几乎所有树的面试题目都会回到BFS或者DFS,使用BFS,None节点存#

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  8. 题目1447:最短路(Floyd算法)

    题目链接:http://ac.jobdu.com/problem.php?pid=1447 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  9. 九度oj 题目1447:最短路

    题目描述: 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线 ...

随机推荐

  1. hi.baidu.com 百度流量统计

    在字幕侠的官网访问之后,发现 <meta name="baidu-site-verification" content="3uvZd9Aact" /> ...

  2. scala学习手记9 - =和==

    = 赋值运算 scala的赋值运算和java的有着很大的不同.如a=b这样的赋值运算,在Java中返回值是a的值,在scala中返回的则是Unit(Unit是值类型,全局只存在唯一的值,即(),通常U ...

  3. Android View的生命周期

    View生命周期相关方法 View是什么?官方源码注释中的定义:这个类是用户接口的基础构件.View表示屏幕上的一块矩形区域,负责绘制这个区域和事件处理. View是所有widget类的基类,Widg ...

  4. poj1330lca入门题

    直接套模板,dfs的时候注意起点 #include<map> #include<set> #include<cmath> #include<queue> ...

  5. poj2478欧拉函数

    打表欧拉函数,求2到n的欧拉函数和 #include<map> #include<set> #include<cmath> #include<queue> ...

  6. IOS-RunTime(刨根问底)

    方法调用 让我们看一下方法调用在运行时的过程(参照前文类在runtime中的表示) 如果用实例对象调用实例方法,会到实例的isa指针指向的对象(也就是类对象)操作.如果调用的是类方法,就会到类对象的i ...

  7. 内存保护机制及绕过方案——通过覆盖虚函数表绕过/GS机制

    1    GS内存保护机制 1.1    GS工作原理 栈中的守护天使--GS,亦称作Stack Canary / Cookie,从VS2003起开始启用(也就说,GS机制是由编译器决定的,跟操作系统 ...

  8. LeetCode OJ:Remove Nth Node From End of List(倒序移除List中的元素)

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  9. LeetCode OJ:Remove Element(移除元素)

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  10. New Concept English three (27)

    35w/m 67 It has been said that everyone lives by selling something. In the light of this statement, ...