杭电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出来了,顺便整理了一下关于大数的东西 其实这是刘汝佳老师在<算法竞赛 经典入门 第二版> 中所讲的模板,代码原封不动写上的, ...
随机推荐
- java_IO流小结
字符流和字节流的主要区别: 1.字节流读取的时候,读到一个字节就返回一个字节: 字符流使用了字节流读到一个或多个字节(中文对应的字节数是两个,在UTF-8码表中是3个字节)时.先去查指定的编码表,将 ...
- Team Queue(多队列技巧处理)
Team Queue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- SRM 588 D2 L3:GameInDarknessDiv2,DFS
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12710 采用DFS搜索,第一次写的时候忘了加访问标志,结果状态 ...
- oracle 格式化数字 to_char
转:http://blog.csdn.net/chinarenzhou/article/details/5748965 Postgres 格式化函数提供一套有效的工具用于把各种数据类型(日期/时间,i ...
- SWTBOK測试实践系列(4) -- 软件測试技术的黑白之道
白盒測试和黑盒測试往往是项目中最受争议的两种測试类型,每一个人偏爱各不同.现实生活中行业人员大多喜欢白盒測试而忽视黑盒測试,那么项目中又应该怎样平衡这两类測试呢?我们先来看两个案例. 案例一: 某移动 ...
- jsp filter登录限制过滤器
http://www.cnblogs.com/hemingwang0902/archive/2012/01/09/2316956.html UserFilter.java package filter ...
- ARM体系结构_DAY2
程序状态寄存器(CPSR) Mode位[4:0]:处理器模式为 USER模式不能直接切换到特权模式,在特权模式下可以直接修改mode位[4:0]为10000,切换到USER模式. T bit位[5]: ...
- 普通用户登录PLSQL后提示空白OK对话框错误
问题描述: 1.普通域账号登录域成员服务器后,打开PLSQL正常,输入用户名密码登录后提示一个空白的OK对话框,点确定后又返回到输入用户密码界面. 2.在CMD窗口下调用SQLPLUS登录数据库时报如 ...
- FileZilla 安装配置参考
http://www.admin10000.com/document/72.html 解决 NAT issue https://wiki.filezilla-project.org/Network_C ...
- C# 模拟键盘按键操作
[DllImport("user32.dll")] public static extern IntPtr keybd_event(byte bVk, byte bScan, in ...