A Walk Through the Forest |
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) |
Total Submission(s): 118 Accepted Submission(s): 57 |
Problem Description
Jimmy experiences a lot of stress at work these days, 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. |
Input
Input contains several test cases followed by a line 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.
|
Output
For each test case, output a single integer indicating the number of different routes through the forest. You may assume that this number does not exceed 2147483647
|
Sample Input
5 6 |
Sample Output
2 |
Source
University of Waterloo Local Contest 2005.09.24
|
Recommend
Eddy
|
/*
最短路的条数
*/
#include<bits/stdc++.h>
#define N 1010
#define INF 0x3f3f3f3f
using namespace std;
int n,m;
int mapn[N][N];
int vis[N];
int dis[N];
int dp[N];//表示到点i的最短路有多少条
int x,y,val;
/*
求出到各点的最短路
*/
void dijkstra(int s){
int i,j,minn,pos;
memset(vis,,sizeof(vis));
for(i = ; i<=n; i++)
dis[i] = mapn[s][i];
dis[s]=;
vis[s]=;
for(i=;i<=n;i++){
minn=INF;
for(j=;j<=n;j++){
if(dis[j]<minn&&!vis[j]){
minn=dis[pos=j];
}
}
/*
如果不加这句话minn没有找到的话就是INF就会越界的
*/
if(minn==INF) break;
vis[pos]=;
for(j=;j<=n;j++){
if(!vis[j]&&dis[pos]+mapn[pos][j]<dis[j]){
dis[j]=dis[pos]+mapn[pos][j];
}
}
}
}
/*
然后记忆化深搜出结果
*/
int dfs(int v){
int sum=;
if(dp[v]!=-) return dp[v];//搜到第v个结点的时候直接拿过来用就行了
if(v==) return ;
for(int i=;i<=n;i++){
/*
状态转移主要在于dis[v]
*/
if(mapn[v][i]!=INF&&dis[v]>dis[i])
sum+=dfs(i);
}
dp[v]=sum;
return dp[v];
}
int main(){
//freopen("C:\\Users\\acer\\Desktop\\in.txt","r",stdin);
while(scanf("%d",&n)!=EOF&&n){
for(int i=;i<=n;i++){
dp[i]=-;
for(int j=;j<=n;j++){
mapn[i][j]=INF;
}
}
scanf("%d",&m);
while(m--){
scanf("%d%d%d",&x,&y,&val);
mapn[x][y]=mapn[y][x]=val;
}
/*将每点到2的最短距离打到dis数组中去*/
dijkstra();
printf("%d\n",dfs());
}
return ;
}
/*
36 0 37 34 24
2
4 0 3 3 1 1 2
4
*/
A Walk Through the Forest的更多相关文章
-
A Walk Through the Forest[HDU1142]
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 ...
-
HDU 1142 A Walk Through the Forest (求最短路条数)
A Walk Through the Forest 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1142 Description Jimmy exp ...
-
UVa 10917 A Walk Through the Forest
A Walk Through the Forest Time Limit:1000MS Memory Limit:65536K Total Submit:48 Accepted:15 Descrip ...
-
hdu_A Walk Through the Forest ——迪杰特斯拉+dfs
A Walk Through the Forest Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/ ...
-
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 ...
-
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 ...
随机推荐
-
Linux 折腾汇集,实时更新
一.Linux教程 入门教程:http://www.92csz.com/study/linux/ 命令大全:http://man.linuxde.net/ 一.界面: 在Ubuntu.Linux Mi ...
-
sublime学习
1:goto anything 特性 快捷键 ctrl + P @ : # 使用 2:多行游标功能 快捷键 ctrl + D ctrl + K 跳过选择 alt + F3 多选 产生 ...
- 【Hadoop】史上最全 Hadoop 生态 全景图
-
unity, 在OnDisable里一定要将Cloth禁掉
如果在OnDisable中不将Cloth组件禁掉,则当物体再次激活时布料将变形.
-
DataTable添加行和列
tablenullobjectdatasetc#c 手动插入一行数据 DataSet ds = tTalent.GetAllInfo(); DataRow dr = ds.Tables ...
-
使用 PIVOT 和 UNPIVOT 行转列 列转行 报表统计 函数
官方文档:http://technet.microsoft.com/zh-cn/library/ms177410(v=SQL.105).aspx 可以使用 PIVOT 和 UNPIVOT 关系运算符将 ...
-
Spinner 实现key value 效果
在使用Spinner进行下拉列表时,我们一般都会使用字符串数组的方式加ArrayAdapter,取到的列表值就是我们所看到的Text.如果我们想实现网页中select <option value ...
-
zabbix备份数据库
#全库备份(数据量大很慢且会告警) mysqldump -uzabbix -pzabbix --opt zabbix | bzip2 > zabbix.sql.bz2 #备份配置表 mysqld ...
-
C++程序设计--实验二
第二次实验主要内容是函数重载,快速排序及其模板实现,简单的user类实现. 实验结论: 一.函数重载编程练习 /*编写重载函数add(),实现对int型,double型和Complex型数据的加法.在 ...
-
python api接口认证脚本
import requests import sys def acces_api_with_cookie(url_login, USERNAME, PASSWORD, url_access): ...