hdu 1142 A Walk Through the Forest

http://acm.split.hdu.edu.cn/showproblem.php?pid=1142




根据题意,将2看成起点反向思考,spfa求出所有点到2的最短路径,在从1开始用记忆化dfs以1为起点找1到2的不同的路





#include<bits/stdc++.h>
using namespace std;
struct r{
	int ed,va;
};
r road[1111];
vector<r> po[1111];
int dp[1111],dis[1111];
bool vis[1111];int n,m;
void spfa(int st)
{
	memset(vis,0,sizeof(vis));
	for(int i=1;i<=n;i++)
	dis[i]=0x3fffffff;
	queue<int> q;
	q.push(st);
	dis[st]=0;
	vis[st]=1;
	while(!q.empty())
	{
		st=q.front();
		q.pop();
		vis[st]=0;
		for(int i=0;i<po[st].size();i++)
		{
			int end=po[st][i].ed;
			int val=po[st][i].va;
			if(dis[st]+val<dis[end])
			{
				dis[end]=dis[st]+val;
				if(vis[end]==0)
				{
					q.push(end);
					vis[end]=1;
				}
			}
		}
	}
}
void add(int u,int v,int w)
{
	r x;
	x.ed=v;x.va=w;
	po[u].push_back(x);
 } 
 int dfs(int st)
 {
 	if(st==2)return 1;
 	 if(dp[st]>0)
        return dp[st];  
    for(int i=0;i<po[st].size();i++)  
    {  
        int end=po[st][i].ed;  
        if (dis[end]<dis[st])
            dp[st]+= dfs(end);  
    }  
    return dp[st];  
 
 }
int main(){
	
	
	while(~scanf("%d",&n))
	{
		for(int i=1;i<=n;i++)
		po[i].clear();
		if(n==0)break;
		cin>>m;
	    int i;
	    int u,v,w;
	    for(i=1;i<=m;i++)
	    {
	    	cin>>u>>v>>w;
	    	add(u,v,w);
	    	add(v,u,w);
		}
		spfa(2);
		memset(dp,0,sizeof(dp));
		int ans=dfs(1);
		printf("%d\n",ans);
	}
	return 0;
	} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值