杭电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出来了,顺便整理了一下关于大数的东西 其实这是刘汝佳老师在<算法竞赛 经典入门 第二版> 中所讲的模板,代码原封不动写上的, ...
随机推荐
- 玩转指针(Playing with Pointers)
Question: What is a Pointer? What are its limitations? What are its benefits? How do we use it? What ...
- Web开发之RSET API
REST介绍 如果要说什么是REST的话,那最好先从Web(万维网)说起. 什么是Web呢?读者可以查看维基百科的词条(http://zh.wikipedia.org/zh-cn/Web),具体的我就 ...
- html5 750 REM JS换算方法
在安卓手机低版本浏览器,如果进页面快速执行的话会出现计算宽度不正确的情况,解决方法是放在onload方法里面执行,但这种解决方式在一些高版本浏览器中会出现页面闪动,所以使用判断浏览器版本的方式来解决, ...
- C# 插入排序算法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 使用markdown语法撰写csdn博客
在CSDN之下写blog无疑是一件非常吃力的事情,对于非常多simple爱好者来讲,能用markdown语法来书写代码是最优雅简洁只是的了.本文主要介绍markdown语法和怎样它来撰写csdn下的b ...
- [置顶] iOS 应用程序内部国际化,不跟随系统语言
前言:网络上关于iOS国际化的文章很多,但基本上都是基于跟随系统语言的国际化,笔者就不赘述了-0 – 今天要讲的是不跟随系统的切换语言版本方案,即程序内部的切换语言版本方案. 一.总则: 应用内部语言 ...
- log4net使用具体解释
说明:本程序演示怎样利用log4net记录程序日志信息.log4net是一个功能著名的开源日志记录组件.利用log4net能够方便地将日志信息记录到文件.控制台.Windows事件日志和数据库(包含M ...
- Android-IM架构设计
###1. 架构总览 ###2. 模块介绍 ####2.1 协议封装与任务流程 #####1) 协议与任务的封装 a. 协议有协议头(协议头因为格式相同,被抽象出来)和协议体组成,协议有两类:请求协议 ...
- 设置单选的listView或者gridview
主要是这个BeaseAdapter的方法notifyDataSetChanged()的使用;作用 :调用BaseAdapter中的getView();方法,刷新ListView中的数据.实现:1.在B ...
- ASP.NET入门 - 错误和异常处理
当发生错误时,应该向用户提供一个友好的界面,可以屏蔽详细技术信息,也有助提升用户体验,这时就用到了异常处理. 错误类型: 语法错误:因缺少关键字,符号,或其他不正确代码所产生的错误,无法通过编译. 逻 ...