【uva10917】Walk Through the Forest (最短路)
题目:
gbn最近打算穿过一个森林,但是他比较傲娇,于是他决定只走一些特殊的道路,他打算只沿着满足如下条件的(A,B)道路走:存在一条从B出发回家的路,比所有从A出发回家的路径都短。你的任务是计算一共有多少条不同的回家路径。其中起点的编号为1,终点的编号为2.
分析:
先求出每个点到终点的距离,根据题目要求找d[u]>d[v]的路径(u,v)走,计算路径数即可。
代码如下:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define Maxn 1010 struct node
{
int x,y,c,next;
}t[*Maxn*Maxn];int len; int n,m;
int first[Maxn],dis[Maxn],f[Maxn];
bool inq[Maxn]; void ins(int x,int y,int c)
{
t[++len].x=x;t[len].y=y;t[len].c=c;
t[len].next=first[x];first[x]=len;
} void spfa(int s)
{
queue<int > q;
while(!q.empty()) q.pop();
q.push(s);
memset(dis,,sizeof(dis));
memset(inq,,sizeof(inq));
dis[s]=;
while(!q.empty())
{
int x=q.front();q.pop();
for(int i=first[x];i;i=t[i].next)
{
int y=t[i].y;
if(dis[y]>dis[x]+t[i].c)
{
dis[y]=dis[x]+t[i].c;
if(!inq[y]) {inq[y]=;q.push(y);}
}
}
inq[x]=;
}
} int ffind(int x)
{
if(f[x]!=-) return f[x];
f[x]=;
for(int i=first[x];i;i=t[i].next) if(dis[x]>dis[t[i].y])
{
f[x]+=ffind(t[i].y);
}
return f[x];
} int main()
{
while()
{
scanf("%d",&n);
if(n==) break;
scanf("%d",&m);
len=;
memset(first,,sizeof(first));
for(int i=;i<=m;i++)
{
int x,y,c;
scanf("%d%d%d",&x,&y,&c);
ins(x,y,c);ins(y,x,c);
}
spfa();
memset(f,-,sizeof(f));
f[]=;
ffind();
printf("%d\n",f[]);
}
return ;
}
uva 10917
2016-03-21 13:49:15
【uva10917】Walk Through the Forest (最短路)的更多相关文章
- A Walk Through the Forest (最短路+记忆化搜索)
Jimmy experiences a lot of stress at work these days, especially since his accident made working dif ...
- Uva10917 Walk Through the Forest
题目链接:https://vjudge.net/problem/UVA-10917 题目意思:Jimmy下班回家要闯过一下森林,劳累一天后在森林中散步是非常惬意的事,所以他打算每天沿着一条不同的路径回 ...
- UVA-10917 Walk Through the Forest (dijkstra+DP)
题目大意:n个点,m条边的无向图.一个人从起点到终点按照下面的走法:从A走向B当A到终点的最小距离比B到终点的最小距离大时.问从起点到终点有多少路径方案. 题目分析:先用dijkstra预处理出终点到 ...
- UVA - 10917 - Walk Through the Forest(最短路+记忆化搜索)
Problem UVA - 10917 - Walk Through the Forest Time Limit: 3000 mSec Problem Description Jimmy exp ...
- HDU 1142 A Walk Through the Forest(最短路+记忆化搜索)
A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- 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 ...
- hduoj----1142A Walk Through the Forest(记忆化搜索+最短路)
A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- HDU 1142 A Walk Through the Forest (记忆化搜索 最短路)
A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- HDU1142 A Walk Through the Forest(最短路+DAG)
A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- HDU 1142 A Walk Through the Forest (求最短路条数)
A Walk Through the Forest 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1142 Description Jimmy exp ...
随机推荐
- Class Fxp\Composer\AssetPlugin\Repository\NpmRepository does not exist
使用 composer 安装 laravel 时报错, 如下: [ReflectionException] Class Fxp\Composer\AssetPlugin\Repository\NpmR ...
- Android源码编译过程之九鼎开发板
build_kernel() { # 进入源码顶层目录 cd ${BS_DIR_KERNEL} || # 编译配置文件 make ${BS_CONFIG_KERNEL} ARCH=arm CROSS_ ...
- 总结如何实现Android浮动层,主要是dialog的使用
自定义一个类继承自Dialog类,然后在构造方法中,定义这个dialog的布局和一些初始化信息. 查看源码打印? 01 public class MenuDialog extends Dialog { ...
- ECshop--导航栏模块细究
花了一下午的时间,总算解决了. 本来想在前台界面上分析看看ecshop导航栏设置,在浏览器里面是定位到了"nav-manu"下面,然后子目录是在m_left下可以看到是动态生成一系 ...
- AngularJS Filter用法详解【转+实际测试】 格式化货币
AngularJS内建了一些常用的Filter,我们一一来看一下. currencyFilter(currency): 用途:格式化货币 方法原型: function(amount, currency ...
- C#DbHelperOra,Oracle数据库帮助类 (转载)
主要功能如下数据访问抽象基础类 主要是访问Oracle数据库主要实现如下功能 .数据访问基础类(基于Oracle),主要是用来访问Oracle数据库的. .得到最大值:是否存在:是否存在(基于Orac ...
- Maven配置jar(war)包自动打包上传Maven服务器的配置
Maven配置jar(war)包自动打包上传Maven服务器的配置 创建jar(war)包工程 创建一个maven工程 在工程中穿件一个测试类 配置pom.xml <distributionMa ...
- Ubuntu 13.04 配置Cocos2d-x记录
装备工作: 下载JavaJDK http://www.oracle.com/technetwork/java/javase/downloads/index.html 下载NDKhttp://deve ...
- 数据库(学习整理)----1--如何彻底清除系统中Oracle的痕迹(重装Oracle时)
1.关于重装Oracle数据库: 由于以前装过Oracle数据库,但是版本不怎么样,结果过了试用期之后,我就没有破解和再找合适的版本了!直接使用电脑管家卸载了!可想而知,肯定没清除Oracle痕迹啊! ...
- red bottom shoes featured
最近做了一个红底高根鞋的电商网站 Cheap Red Bottom Shoes Christian Louboutin Loafers Bestsellers Christian Louboutin ...