最短路

来源:互联网 发布:九死惊陵甲 知乎 编辑:程序博客网 时间:2024/04/30 18:11

A Walk Through the Forest

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 7   Accepted Submission(s) : 4
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 61 3 21 4 23 4 31 5 124 2 345 2 247 81 3 11 4 13 7 17 4 17 5 16 7 15 2 16 2 10
 

Sample Output
24
 
题意:寻找一共有多少条符合题意的路。即:能从A点走到点B(点A到终点的最短路>点B到终点的最短路)因此,从终点出发,求每一个点的最短路(dijkstra()),然后那些最短路的值记录起来,作为能否通过的判断条件。最后用记忆化搜索来搜索出一共多少条符合条件的路(dfs());#include<iostream>#include<cstdio>#include<string.h>#define maxint 0xfffffff#define max 1001using namespace std;int n,m;//十字路口数int map[max][max];int dis[max];int vis[max];int d[max];void init(){    int i,j;    for(i=1;i<max;i++)     for(j=1;j<max;j++)     {         map[i][j]=maxint;     }}void dijkstra(int tem){    int i,j;    for(i=1;i<=n;i++)    {        vis[i]=0;        dis[i]=map[i][tem];    }    vis[tem]=1;    dis[tem]=0;    for(i=1;i<n;i++)    {        int mid=0;        int d=maxint;       for(j=1;j<=n;j++)       {           if(vis[j]==0&&d>dis[j])           {               d=dis[j];               mid=j;           }       }       vis[mid]=1;       if(d==maxint)         break;       for(j=1;j<=n;j++)       {           if(vis[j]==0&&map[j][mid]<maxint&&map[j][mid]+d<dis[j])           dis[j]=map[j][mid]+d;       }    }}int dfs(int v){    int i;    if(v==2)     return 1;    if(d[v])     return d[v];    int ans=0,temp;    for(i=1;i<=n;i++)    {      if(map[v][i]!=maxint&&dis[v]>dis[i])//有路相通,要去的i点到终点的距离比v点到终点的距离小      {          temp=dfs(i);          ans+=temp;      }    }    d[v]=ans;    return ans;}int main(){    int a,b,di;    while(scanf("%d",&n),n)    {        init();        scanf("%d",&m);        for(int i=0;i<m;i++)        {            scanf("%d%d%d",&a,&b,&di);             map[a][b]=map[b][a]=di;        }        memset(d,0,sizeof(d));        dijkstra(2);        printf("%d\n",dfs(1));    }    return 0;}


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 电工超作证丢了怎么办 设计师直接找电梯厂家怎么办 研究生补助申请期限过了怎么办 我的电脑图标没了怎么办 苹果锁频密码忘了怎么办 孕妇被降职降薪怎么办 公司降职降薪员工不同意怎么办 企业因为经营不善要降薪该怎么办 调岗不降薪我该怎么办? 怀孕后强制调岗怎么办 有限公司法人变更后债务怎么办 有限公司法人跑路债务怎么办 网上买票身份信息待核验怎么办 微信买票身份核验失败怎么办 买高铁票待核验怎么办 网上购票身份待核验怎么办 b站稿件版权原因怎么办 已离职老板打电话说账有问题怎么办 开到应急刹车道怎么办 自动挡的车刹车失灵怎么办 自动挡的车如果刹车失灵怎么办 手动挡汽车刹车失灵怎么办 车辆没有年检出了交通事故怎么办 跟着大货车闯了红灯怎么办 在万家金服买的电子产品坏了怎么办 汽车被油笔画了怎么办? 挂到别人车跑了怎么办 浪琴手表保修卡掉了怎么办 事故车辆维修和报废怎么办 4.2货车拉缸了怎么办 工作中与同事发生矛盾怎么办 和领导关系闹僵怎么办 内倒窗户卡住了怎么办 支付宝存在安全风险怎么办 地铁车站空调坏了怎么办 面试防汛值班发生灾情你怎么办 怀孕上班路途太远怎么办 硕士错过校招应该怎么办 收银员收多了钱怎么办 商铺贷款批不了怎么办 铁路局的门面乱收房租怎么办