【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 ...
随机推荐
- 【转】jsoncpp在xcode中的使用
http://blog.csdn.net/ashqal/article/details/8573392 考虑到cocos2dx需要使用jsoncpp做关卡的设置, 尝试用源代码直接放到项目以方便后期生 ...
- Memcached Java Client with sample program--reference
In my previous post, I listed down most common telnet commands for memcached with sample execution t ...
- (转载)SVN 提交操作缩写(A D M R) .
前言: 今天使用SVN提交代码,发现提交后的代码找不到之前的版本. 操作的字母缩写为R.一般我们常见的操作为 A D M R A:add,新增 C:conflict,冲突 D:delete,删除 ...
- 使用 git 进行项目管理(只管理代码,不管理项目配置)
使用Git进行项目管理 1. 从服务器pull项目,本地还原工程 从服务器拉取仓库及分支 git clone git@github.com/helloWorld.git git branch -a g ...
- JDK7 HashMap源码分析
本文基于JDK1.7.0_79的版本进行分析. 注释比较详细. 若有不明白的地方可以指出, 我再进行细化. public class HashMap<K,V> extends Abstra ...
- hp惠普服务器监控硬盘
惠普 hpssacli 工具使用 查看raid卡信息(包括控制器状态.Cache状态.电池状态) # hpssacli ctrl all show status 查看raid详细信息 # hpssac ...
- 科讯CMS V9标签清单
全新整理V9标签清单 ====================网站通用标签============== {$GetSiteTitle} 显示网站标题 {$GetSiteName} 显示网站名称 {$G ...
- div随另一个div自动增高
<script type="text/javascript"> document.getElementById("div1").style.heig ...
- gulp 初体验
1,全局安装 gulp npm install --global gulp 但是一直无法成功,后来才知被墙了,于是使用了如下命令,安装cnpm npm install -g cnpm --regist ...
- volatile用处说明
在JDK1.2之前,Java的内存模型实现总是从主存(即共享内存)读取变量,是不需要进行特别的注意的.而随着JVM的成熟和优化,现在在多线程环境下volatile关键字的使用变得非常重要. 在当前 ...