HDU 1142 A Walk Through the Forest(dijkstra+记忆化DFS)
题意:
给你一个图,找最短路。但是有个非一般的的条件:如果a,b之间有路,且你选择要走这条路,那么必须保证a到终点的所有路都小于b到终点的一条路。问满足这样的路径条数 有多少,噶呜~~题意是搜了解题报告才明白的Orz.。。。英语渣~
思路:
1.1为起点,2为终点,因为要走ab路时,必须保证那个条件,所以从终点开始使用单源最短路Dijkstra算法,得到每个点到终点的最短路,保存在dis[]数组中。
2.然后从起点开始深搜每条路,看看满足题意的路径有多少条。
3.这样搜索之后,dp[1]就是从起点到终点所有满足题意的路径的条数。
AC_CODE:
#include<stdio.h>
#include<string.h>
#define INF 10000000
int map[][];
int vis[];
int dis[];
int n,m;
int dp[];
void dijkstra(int st)
{
int i,j,k;
memset(vis,,sizeof(vis));
for(i=;i<=n;i++)
{
if(i==st)
dis[i]=;
else
dis[i]=INF;
}
for(i=;i<=n;i++)
{
int r;
int min=INF;
for(j=;j<=n;j++)
{
if(!vis[j]&&dis[j]<min)
{
min=dis[j];
r=j;
}
}
vis[r]=;
for(k=;k<=n;k++)
{
if(dis[k]>dis[r]+map[r][k])
dis[k]=dis[r]+map[r][k];
}
}
return;
}
int DFS(int st)
{
int sum=;
if(dp[st]!=-)
return dp[st];
if(st==)
return ;
for(int i=;i<=n;i++)
{
if(map[st][i]!=INF&&dis[st]>dis[i])
sum+=DFS(i);
}
dp[st]=sum;
return dp[st];
}
int main()
{
int i,j,u,v,w;
while(scanf("%d",&n)!=EOF&&n)
{
for(i=;i<=n;i++)
{
dp[i]=-;
for(j=;j<=n;j++)
{
if(i==j)
map[i][j]=;
else
map[i][j]=INF;
}
}
scanf("%d",&m);
for(i=;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&w);
map[u][v]=map[v][u]=w;
}
dijkstra();
printf("%d\n",DFS());
}
return ;
}
————Anonymous.PJQ
HDU 1142 A Walk Through the Forest(dijkstra+记忆化DFS)的更多相关文章
- HDU 1142 A Walk Through the Forest(Dijkstra+记忆化搜索)
题意:看样子很多人都把这题目看错了,以为是求最短路的条数.真正的意思是:假设 A和B 是相连的,当前在 A 处, 如果 A 到终点的最短距离大于 B 到终点的最短距离,则可以从 A 通往 B 处,问满 ...
- 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 ...
- HDU 1142 A Walk Through the Forest (求最短路条数)
A Walk Through the Forest 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1142 Description Jimmy exp ...
- 题解报告:hdu 1142 A Walk Through the Forest
题目链接:acm.hdu.edu.cn/showproblem.php?pid=1142 Problem Description Jimmy experiences a lot of stress a ...
- 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(最短路+记忆化搜索)
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 ...
- 【解题报告】HDU -1142 A Walk Through the Forest
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1142 题目大意:Jimmy要从办公室走路回家,办公室在森林的一侧,家在另一侧,他每天要采取不一样的路线 ...
- hdu 1142 A Walk Through the Forest
http://acm.hdu.edu.cn/showproblem.php?pid=1142 这道题是spfa求最短路,然后dfs()求路径数. #include <cstdio> #in ...
随机推荐
- javascript每日一练(七)——事件二:键盘事件
一.键盘事件 onkeydown触发, keyCode键盘编码 ctrlKey altKey shiftKey 键盘控制div移动 <!doctype html> <html> ...
- android之View的启动过程
转自:http://www.cdtarena.com/gpx/201308/9607.html 程序里调用了onSizeChanged方法进行了一些设置,不知道onSizeChanged是在什么时候启 ...
- 你知道hover、active这四个伪类为什么要按顺序写吗
刨根问底,你知道:hover等4个伪类为什么要按顺序排列吗 引言 :link,:visited,:hover,:active这4个伪类大家都不陌生,4个伪类要按照LvHa这个爱恨原则来排(外国友人起的 ...
- admin嵌套在spring mvc项目里,菜单栏点击新连接每次都会重置
<ul class="treeview-menu" id="ul_schedule"> <li><a href="#&q ...
- Java I/O流-PipedInputStream、PipedOutputStream
一.整体代码图 PipedStreamDemo.java import java.io.*; class PipedStreamDemo { public static void main(Strin ...
- 怎样实现多文件上传 在iOS开发中
NSURL* url = [NSURL URLWithString:@"xxx"]; ASIFormDataRequest* request = [ASIFormDataReque ...
- HDU 4336 Card Collector(动态规划-概率DP)
Card Collector Problem Description In your childhood, do you crazy for collecting the beautiful card ...
- JNI 详细解释
JNI事实上,Java Native Interface缩写,那是,java本地接口.它提供了许多API实现和Java和其它语言的通信(主要是C&C++). 或许不少人认为Java已经足够强大 ...
- wcf 出现 IsContentTypeSupported 错误
查看添加的服务地址是不是https开头的,而 *.config 文件里面自动添加的链接变成了http,当前的bindbing类型为basicHttpBinding, 解决方法:在config文件里面手 ...
- Spring Tool Suite(简称STS)针对SimpleDateFormat.pase函数的实参值不做检验,异常直接默认值之
Spring Tool Suite(简称STS)是 Spring 团队开发的一款基于Eclipse的IDE,旨在简化开发Spring MVC 应用的流程.可以自动生成spring相关的配置文件.比如a ...