hdu 1142 A Walk Through the Forest
http://acm.hdu.edu.cn/showproblem.php?pid=1142
这道题是spfa求最短路,然后dfs()求路径数。
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
#define maxn 1001
using namespace std;
const int inf=<<; int g[maxn][maxn];
int dis[maxn];
bool vis[maxn],visi[maxn];
int n,m,a,b,d;
int di;
int cnt[maxn];
int num[maxn]; bool spfa()
{
queue<int>q;
memset(vis,false,sizeof(vis));
memset(cnt,,sizeof(cnt));
for(int i=; i<=n; i++) dis[i]=inf;
dis[]=;
vis[]=true;
q.push();
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=false;
for(int i=; i<=n; i++)
{
if(dis[i]>dis[u]+g[u][i]&&g[u][i]!=inf)
{
dis[i]=dis[u]+g[u][i];
if((++cnt[i])>n) return false;
if(!vis[i])
{
q.push(i);
vis[i]=true;
}
}
}
}
return true;
} void dfs(int src)
{
if(src==)
{
num[]=;
return ;
}
int sum=;
for(int i=; i<=n; i++)
{
if(g[src][i]!=inf&&dis[src]>dis[i])
{
if(num[i]>=)
{
sum+=num[i];
}
else
{
dfs(i);
sum+=num[i];
}
}
}
num[src]=sum;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
if(n==) break;
scanf("%d",&m);
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
if(i==j) g[i][j]=;
else g[i][j]=inf;
}
}
for(int i=; i<m; i++)
{
scanf("%d%d%d",&a,&b,&d);
g[a][b]=g[b][a]=min(g[a][b],d);
}
spfa();
//printf("%d\n",dis[1]);
for(int i=; i<=n; i++)
{
num[i]=-;
}
dfs();
printf("%d\n",num[]);
}
return ;
}
hdu 1142 A Walk Through the Forest的更多相关文章
- 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 ...
 - 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
		
题目链接:acm.hdu.edu.cn/showproblem.php?pid=1142 Problem Description Jimmy experiences a lot of stress a ...
 - 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
		
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1142 题目大意:Jimmy要从办公室走路回家,办公室在森林的一侧,家在另一侧,他每天要采取不一样的路线 ...
 - HDU 1142 A Walk Through the Forest(SPFA+记忆化搜索DFS)
		
题目链接 题意 :办公室编号为1,家编号为2,问从办公室到家有多少条路径,当然路径要短,从A走到B的条件是,A到家比B到家要远,所以可以从A走向B . 思路 : 先以终点为起点求最短路,然后记忆化搜索 ...
 - HDU 1142 A Walk Through the Forest(dijkstra+记忆化DFS)
		
题意: 给你一个图,找最短路.但是有个非一般的的条件:如果a,b之间有路,且你选择要走这条路,那么必须保证a到终点的所有路都小于b到终点的一条路.问满足这样的路径条数 有多少,噶呜~~题意是搜了解题报 ...
 
随机推荐
- 技巧两种:LINUX删除指定后缀文件及PYTHON更改屏幕字色
			
http://blog.csdn.net/caryaliu/article/details/8753028 http://www.iitshare.com/python-print-color-log ...
 - Codeforces 449D Jzzhu and Numbers
			
http://codeforces.com/problemset/problem/449/D 题意:给n个数,求and起来最后为0的集合方案数有多少 思路:考虑容斥,ans=(-1)^k*num(k) ...
 - ISO7816通讯协议在工控主板EM9160中的实现方案
			
在新的国家电网智能终端相关标准中,规定了通过专门的加密芯片来保证设备数据安全性的方法,而设备主控单元与加密芯片采用了广泛应用的ISO7816通讯协议.工控主板EM9160为了适应这一新的技术需求,对其 ...
 - 自制单片机之十八……无线通讯模块NRF24L01+
			
(一)基础知识篇 今天刚调试好,先看图吧! 这张是AT89C2051控制NRF24L01+做发射调试. 看看NRF24L01细节吧! 这是LCD屏显示: AT89S52做接收测试: 正在接收时的显示: ...
 - PowerShell 字符串操作符
			
字符串操作符 格式化操作符 –F 在PowerShell文本操作符中非常重要,经常被用来增强数字类型和日期类型的可读性: "{0} diskettes per CD" -f (72 ...
 - Android 获取文件大小
			
android 获取文件夹.文件的大小 以B.KB.MB.GB 为单位 FileSizeUtil public class FileSizeUtil { ;//获取文件大小单位为B的double值 ; ...
 - 微软CEO史蒂夫·鲍尔默(Steve Ballmer)在12个月内退休
			
Microsoft CEO Steve Ballmer to retire within 12 months Aug. 23, 2013 Board of directors initiates su ...
 - BZOJ2023: [Usaco2005 Nov]Ant Counting 数蚂蚁
			
2023: [Usaco2005 Nov]Ant Counting 数蚂蚁 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 56 Solved: 16[S ...
 - 齐B小短裙
			
女模周蕊微博引关注 火了齐B小短裙毁了干爹[图]_网易新闻中心 齐B小短裙
 - MySql按日期时间段进行统计(前一天、本周、某一天、某个时间段)
			
在mysql数据库中,常常会遇到统计当天的内容.例如,在user表中,日期字段为:log_time 统计当天 sql语句为: select * from user where date(log_tim ...