题目:

  gbn最近打算穿过一个森林,但是他比较傲娇,于是他决定只走一些特殊的道路,他打算只沿着满足如下条件的(A,B)道路走:存在一条从B出发回家的路,比所有从A出发回家的路径都短。你的任务是计算一共有多少条不同的回家路径。其中起点的编号为1,终点的编号为2.

分析:

  先求出每个点到终点的距离,根据题目要求找d[u]>d[v]的路径(u,v)走,计算路径数即可。

代码如下:

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define Maxn 1010 struct node
{
int x,y,c,next;
}t[*Maxn*Maxn];int len; int n,m;
int first[Maxn],dis[Maxn],f[Maxn];
bool inq[Maxn]; void ins(int x,int y,int c)
{
t[++len].x=x;t[len].y=y;t[len].c=c;
t[len].next=first[x];first[x]=len;
} void spfa(int s)
{
queue<int > q;
while(!q.empty()) q.pop();
q.push(s);
memset(dis,,sizeof(dis));
memset(inq,,sizeof(inq));
dis[s]=;
while(!q.empty())
{
int x=q.front();q.pop();
for(int i=first[x];i;i=t[i].next)
{
int y=t[i].y;
if(dis[y]>dis[x]+t[i].c)
{
dis[y]=dis[x]+t[i].c;
if(!inq[y]) {inq[y]=;q.push(y);}
}
}
inq[x]=;
}
} int ffind(int x)
{
if(f[x]!=-) return f[x];
f[x]=;
for(int i=first[x];i;i=t[i].next) if(dis[x]>dis[t[i].y])
{
f[x]+=ffind(t[i].y);
}
return f[x];
} int main()
{
while()
{
scanf("%d",&n);
if(n==) break;
scanf("%d",&m);
len=;
memset(first,,sizeof(first));
for(int i=;i<=m;i++)
{
int x,y,c;
scanf("%d%d%d",&x,&y,&c);
ins(x,y,c);ins(y,x,c);
}
spfa();
memset(f,-,sizeof(f));
f[]=;
ffind();
printf("%d\n",f[]);
}
return ;
}

uva 10917

2016-03-21 13:49:15

【uva10917】Walk Through the Forest (最短路)的更多相关文章

  1. A Walk Through the Forest (最短路+记忆化搜索)

    Jimmy experiences a lot of stress at work these days, especially since his accident made working dif ...

  2. Uva10917 Walk Through the Forest

    题目链接:https://vjudge.net/problem/UVA-10917 题目意思:Jimmy下班回家要闯过一下森林,劳累一天后在森林中散步是非常惬意的事,所以他打算每天沿着一条不同的路径回 ...

  3. UVA-10917 Walk Through the Forest (dijkstra+DP)

    题目大意:n个点,m条边的无向图.一个人从起点到终点按照下面的走法:从A走向B当A到终点的最小距离比B到终点的最小距离大时.问从起点到终点有多少路径方案. 题目分析:先用dijkstra预处理出终点到 ...

  4. UVA - 10917 - Walk Through the Forest(最短路+记忆化搜索)

    Problem    UVA - 10917 - Walk Through the Forest Time Limit: 3000 mSec Problem Description Jimmy exp ...

  5. 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 ...

  6. 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 ...

  7. hduoj----1142A Walk Through the Forest(记忆化搜索+最短路)

    A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  8. 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 ...

  9. 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 ...

  10. HDU 1142 A Walk Through the Forest (求最短路条数)

    A Walk Through the Forest 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1142 Description Jimmy exp ...

随机推荐

  1. &&与&

    if((2>1)&&(4>3))System.out.printf("两边都是true"); else   System.out.println(&qu ...

  2. thinking in java 读书笔记 --- overriding private method

    一个对象可以被用作它自身的类型或者是它的基类类型.当用作它的基类类型时叫做upcasting,如下所示 BaseClass ref = new DerivedClass()  //upcasting ...

  3. xhEditor与Java结合使用

    xhEditor是一个轻量级的html编辑器,使用它可以非常方便的编辑图文内容,然而官方文档中只有php的演示,没有Java版的,最近两天参考网上各种各样的文档,琢磨了一下用法,现已可以正常运行,现在 ...

  4. a标签中的href="#"与href="javascript:void(0)"区别

    转自http://blog.csdn.net/fightplane/article/details/5190037 <a href="#"> 点击链接后,页面会向上滚到 ...

  5. 第一篇:杂项之pymysql连接池

    杂项之pymysql连接池   杂项之pymysql连接池 本节内容 本文的诞生 连接池及单例模式 多线程提升 协程提升 后记 1.本文的诞生 由于前几天接触了pymysql,在测试数据过程中,使用普 ...

  6. Android 指定日期时间执行任务的Timer

    放上一个指定详细日期及时间的timer public class MainActivity extends Activity { private Handler handler = new Handl ...

  7. sql server split函数

    --创建分割函数CREATE FUNCTION dbo.Split(@String varchar(8000),@Delimiter char(1))returns @temptable TABLE ...

  8. ubuntu出现有线已连接却无法上网

    或者直接追加到/etc/sysctl.conf 如果遇到“设备未托管”,一般是台式机默认移动ip后禁用网络. 那么修改/etc/NetworkManager/NetworkManager.conf,设 ...

  9. ASP.NET页面生命周期总结(完结篇)

    补充: W3svc服务  负责把‘工作进程’启动起来 W3svc 连接工作进程.内核模块.IIS 主服务的一个核心的桥梁 W3svc还有一个作用就是维护应用程序池,可以设置多长时间回收,多长时间重启. ...

  10. UIButton 使用imageEdgeInsets和titleEdgeInsets属性

    现在App的底部栏.侧边栏.顶部栏经常出现一些包含图像和文字的Item,以前用按钮上面添加label和imageView,  想想实在是对资源的浪费..                 图1 — 底部 ...