A Walk Through the Forest(最短路径+DFS)

来源:互联网 发布:unity3d做2d游戏 编辑:程序博客网 时间:2024/05/19 13:58

A Walk Through the Forest

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 123 Accepted Submission(s): 59 
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
#include <stdio.h>#include <iostream>#include <queue>#include <vector>#include <cmath>#include <algorithm>#include <string.h>#define INF 0x3fffffff#define MAX_N 5005#define MAX_E 5005#define MAX_V 50005using namespace std;typedef long long ll;typedef pair<int, int> pp;struct edge {    int to;    int w;};edge e;int u, v, w;int V, E;int N, M, R;vector<edge> G[MAX_V];int d[MAX_V];int ans[MAX_V];int vis[MAX_V];void dijkstra(int s) {    priority_queue<pp, vector<pp>, greater<pp> > q;    fill(d, d+V+1, INF);    d[s] = 0;    q.push(pp(0, s));    while (q.size()) {        pp p = q.top();        q.pop();        int v = p.second;        int w = p.first;        if (d[v] < w) continue;        for (int i = 0; i < G[v].size(); i++) {            edge e = G[v][i];            if (d[e.to] > d[v] + e.w) {                d[e.to] = d[v] + e.w;                q.push(pp(d[e.to], e.to));            }        }    }}int dfs(int v) {    int sum = 0;    if (ans[v] != -1) return ans[v];    // 1到达2    if (v == 2) return 1;    for (int i = 0; i < G[v].size(); i++) {        edge e = G[v][i];        // 如果a,b之间有路,且你选择要走这条路,那么必须保证a到终点的所有路都小于b到终点的一条路        if (d[v] > d[ e.to ]) {            sum += dfs(e.to);        }    }    return ans[v] = sum;}int main(void){  //  freopen("in.txt", "r", stdin);    while (~scanf("%d%d", &V, &E)) {        if (V == 0) break;        memset(G, 0, sizeof(G));        for (int i = 0; i < E; i++) {            scanf("%d%d%d", &u, &v, &e.w);            e.to = v;            G[u].push_back(e);            e.to = u;            G[v].push_back(e);        }        // 得到顶点2到其他顶点的最短距离        dijkstra(2);        memset(ans, -1, sizeof(ans));        printf("%d\n", dfs(1));    }    return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 实体店买到翻新苹果手机怎么办 太阳花叶子蔫了怎么办 刚摘下来的多肉怎么办 购车4s不给合同怎么办 橙光游戏2.0商城怎么办 飞羽花卷叶了怎么办 孔雀竹芋叶子发黄卷曲怎么办 孔雀竹芋叶子黄了卷了怎么办 飞羽竹芋叶尖黄怎么办 十七岁还是胸小怎么办 英雄联盟连接不上服务器怎么办 彩叶草叶子蔫了怎么办 家里种葱老是死怎么办 非洲菊生虫子了怎么办 多肉的花剪下来怎么办 结石痛怎么办怎么止疼 喝玫瑰花茶胃疼怎么办 卡地亚戒指掉色怎么办 苹果8plus掉电快怎么办 苹果8plus耗电快怎么办 卡地亚戒指划痕怎么办 苹果手机玫瑰金掉色怎么办 14k玫瑰金掉色怎么办 卡地亚手镯掉色怎么办 手机掉油漆里了怎么办 黄金戴久了变黑怎么办 玫瑰金褪色不亮了怎么办 黄金带久了不亮怎么办 玉石带久了不亮怎么办 手表带久了不亮怎么办 蜜蜡带久了不亮怎么办 钛钢首饰不亮了怎么办 潘多拉玫瑰金戒指褪色了怎么办 金色手表漆掉了怎么办 玫瑰金表带褪色后怎么办 K金褪色或泛黄怎么办 钛钢玫瑰金变黑怎么办 玫瑰金手镯掉色了怎么办 彩金颜色不亮了怎么办 玫瑰金链子黑了怎么办 18k玫瑰金变黑了怎么办