杭电1142(最短路径+dfs)
A Walk Through the Forest
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5421 Accepted Submission(s):
1988
especially since his accident made working difficult. To relax after a hard day,
he likes to walk home. To make things even nicer, his office is on one side of a
forest, and his house is on the other. A nice walk through the forest, seeing
the birds and chipmunks is quite enjoyable.
The forest is beautiful, and
Jimmy wants to take a different route everyday. He also wants to get home before
dark, so he always takes a path to make progress towards his house. He considers
taking a path from A to B to be progress if there exists a route from B to his
home that is shorter than any possible route from A. Calculate how many
different routes through the forest Jimmy might take.
containing 0. Jimmy has numbered each intersection or joining of paths starting
with 1. His office is numbered 1, and his house is numbered 2. The first line of
each test case gives the number of intersections N, 1 < N ≤ 1000, and the
number of paths M. The following M lines each contain a pair of intersections a
b and an integer distance 1 ≤ d ≤ 1000000 indicating a path of length d between
intersection a and a different intersection b. Jimmy may walk a path any
direction he chooses. There is at most one path between any pair of
intersections.
the number of different routes through the forest. You may assume that this
number does not exceed 2147483647
#include<stdio.h>
#include<stdlib.h>
#include<string.h> #define INF 0xfffffff int map[][], disk[], pathnum[];
int n, m; int getmin(int x, int y){
return x > y ? y : x;
} int dis(){
int i, j, visit[], idmin, min;
memset(visit, , sizeof(visit));
for(i = ; i <= n; i ++){
disk[i] = map[][i];
}
disk[] = ;
for(i = ; i <= n; i ++){
idmin = ;
min = INF;
for(j = ; j <= n; j ++){
if(!visit[j] && disk[j] < min){
min = disk[j];
idmin = j;
}
}
visit[idmin] = ;
for(j = ; j <= n; j ++){
if(!visit[j]){
disk[j] = getmin(disk[j], map[idmin][j] + disk[idmin]);
}
}
}
return ;
} int dfs(int start){
int sum, i;
if(start == ){
return ;
}
if(pathnum[start] != -){
return pathnum[start];
}
sum = ;
for(i = ; i <= n; i ++){
if(map[i][start] != INF && map[i][start] == disk[start] - disk[i]){
sum += dfs(i);
}
}
pathnum[start] = sum;
return pathnum[start];
} int main(){
int x, y, d, i, j;
while(scanf("%d", &n) && n){
scanf("%d", &m);
for(i = ; i < ; i ++){
for(j = ; j < ; j ++){
map[i][j] = INF;
}
}
//printf("%d\n", map[1][1]);
for(i = ; i < m; i ++){
scanf("%d %d %d", &x, &y, &d);
map[x][y] = map[y][x] = d;
}
dis();
//printf("%d\n", disk[1]);
memset(pathnum, -, sizeof(pathnum));
//printf("%d\n", pathnum[1]);
printf("%d\n", dfs());
}
return ;
}
杭电1142(最短路径+dfs)的更多相关文章
- 杭电1010(dfs + 奇偶剪枝)
题目: The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked ...
- Sum It Up POJ 1564 HDU 杭电1258【DFS】
Problem Description Given a specified total t and a list of n integers, find all distinct sums using ...
- 杭电ACM分类
杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...
- 杭电ACM题单
杭电acm题目分类版本1 1002 简单的大数 1003 DP经典问题,最大连续子段和 1004 简单题 1005 找规律(循环点) 1006 感觉有点BT的题,我到现在还没过 1007 经典问题,最 ...
- ACM 五一杭电赛码"BestCoder"杯中国大学生程序设计冠军赛小记
对于这项曾经热爱的竞赛,不得不说这是我最后一年参加ACM比赛了,所以要珍惜每一次比赛的机会. 五一去杭电参加了赛码"BestCoder"杯中国大学生程序设计冠军赛,去的队伍包括了今 ...
- 『ACM C++』HDU杭电OJ | 1415 - Jugs (灌水定理引申)
今天总算开学了,当了班长就是麻烦,明明自己没买书却要带着一波人去领书,那能怎么办呢,只能说我善人心肠哈哈哈,不过我脑子里突然浮起一个念头,大二还要不要继续当这个班委呢,既然已经体验过就可以适当放下了吧 ...
- 一个人的旅行 HDU杭电2066【dijkstra算法 || SPFA】
pid=2066">http://acm.hdu.edu.cn/showproblem.php? pid=2066 Problem Description 尽管草儿是个路痴(就是在杭电 ...
- acm入门 杭电1001题 有关溢出的考虑
最近在尝试做acm试题,刚刚是1001题就把我困住了,这是题目: Problem Description In this problem, your task is to calculate SUM( ...
- 杭电acm 1002 大数模板(一)
从杭电第一题开始A,发现做到1002就不会了,经过几天时间终于A出来了,顺便整理了一下关于大数的东西 其实这是刘汝佳老师在<算法竞赛 经典入门 第二版> 中所讲的模板,代码原封不动写上的, ...
随机推荐
- poj1185:炮兵阵地(状压dp)
也算是比较基础的状压dp了,跟做过的第二道比较又稍微复杂了一点 需要记录之前两行的状态.. 统计结果也稍有不同 另外还学习了一个得到一个整数二进制位 1 的个数的位运算方法 详见代码: #includ ...
- socket 网摘
一.基本socket函数 Linux系统是通过提供套接字(socket)来进行网络编程的.网络的socket数据传输是一种特殊的I/O,socket也是一种文件描述符.socket也有一个类似于打 开 ...
- python lcd 时间显示
#!/usr/bin/python # QStopWatch -- a very simple stop watch # Copyright (C) 2006 Dominic Battre <d ...
- WebService-使用JDK开发WebService
一.使用JDK开发WebService 2.1.开发WebService服务器端 1.定义一个interface,使用@WebService注解标注接口,使用@WebMethod注解标注接口中定义的所 ...
- Laravel-表单篇-零散信息
1.asset('path'):用于引入静态文件,包括css.js.img 2.分页,调用模型的paginate(每页显示的行数)方法, 如$student = Student::paginate(2 ...
- jQuery限制文本框只能输入正整数
//限制键盘只能按数字键.小键盘数字键.退格键 $("#txtQty").keydown(function (e) { var code = parseInt(e.keyCode) ...
- OCP-1Z0-051-题目解析-第28题
28. Which two statements are true regarding constraints? (Choose two.) A. A foreign key cannot cont ...
- CSS元素 之 float
1. float 设计的初衷 Float 设计的初衷是为了文字环绕的效果 使得文字可以围绕着 图片.就像下面这样 2. float 的包裹和 破坏 A) 包裹性 和 破坏性 例如下图 我们原本是希 ...
- Server.HTMLEncode用法
Server.HTMLEncode用法!! Server.HTMLEncode HTMLEncode 一.HTMLEncode 方法对指定的字符串应用 HTML 编码. 语法 Server.HTMLE ...
- SQL Server类型与C#类型对应关系
SQL类型 C#类型 bit bool tinyint byte smallint short int int bigint long real float float double money de ...