zoukankan      html  css  js  c++  java
  • UVA

    Problem    UVA - 10917 - Walk Through the Forest

    Time Limit: 3000 mSec

    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 6 1 3 2 1 4 2 3 4 3 1 5 12 4 2 34 5 2 24 7 8 1 3 1 1 4 1 3 7 1 7 4 1 7 5 1 6 7 1 5 2 1 6 2 1 0

    Sample Output

    2

    4

    题解:满足条件的道路<A, B>其实就是满足式子d[B] < d[A],因此跑一边最短路之后,可行路径就出来了,显然只保留可行路径的图是DAG,有向是肯定的,无环也很好理解,对于环上的节点,按照顺时针(或者逆时针)的顺序始终满足上述不等式,绕一圈之后会出现d[s] < d[s],这样的矛盾不等式,所以无环,DAG上统计路径就很简单了,记忆化搜索呗。

      1 #include <bits/stdc++.h>
      2 
      3 using namespace std;
      4 
      5 #define REP(i, n) for (int i = 1; i <= (n); i++)
      6 #define sqr(x) ((x) * (x))
      7 
      8 const int maxn = 1000 + 50;
      9 const int maxm = 1000000 + 100;
     10 const int maxs = 10000 + 10;
     11 
     12 typedef long long LL;
     13 typedef pair<int, int> pii;
     14 typedef pair<double, double> pdd;
     15 
     16 const LL unit = 1LL;
     17 const int INF = 0x3f3f3f3f;
     18 const LL mod = 1000000007;
     19 const double eps = 1e-14;
     20 const double inf = 1e15;
     21 const double pi = acos(-1.0);
     22 
     23 struct Edge
     24 {
     25     int to, next, w;
     26 } edge[maxm];
     27 
     28 struct HeapNode
     29 {
     30     int dis, u;
     31     bool operator<(const HeapNode &a) const
     32     {
     33         return dis > a.dis;
     34     }
     35 };
     36 
     37 int tot, head[maxn];
     38 
     39 void AddEdge(int u, int v, int w)
     40 {
     41     edge[tot].to = v;
     42     edge[tot].next = head[u];
     43     edge[tot].w = w;
     44     head[u] = tot++;
     45 }
     46 
     47 int st, en, n, m;
     48 int dist[maxn];
     49 bool vis[maxn];
     50 
     51 void Dijkstra()
     52 {
     53     for (int i = 0; i <= n; i++)
     54     {
     55         dist[i] = INF;
     56         vis[i] = false;
     57     }
     58     dist[en] = 0;
     59     priority_queue<HeapNode> que;
     60     que.push((HeapNode){0, en});
     61     while (!que.empty())
     62     {
     63         HeapNode x = que.top();
     64         que.pop();
     65         if (vis[x.u])
     66             continue;
     67         int u = x.u;
     68         vis[u] = true;
     69         for (int i = head[u]; i != -1; i = edge[i].next)
     70         {
     71             int v = edge[i].to;
     72             if (dist[v] > dist[u] + edge[i].w)
     73             {
     74                 dist[v] = dist[u] + edge[i].w;
     75                 que.push((HeapNode){dist[v], v});
     76             }
     77         }
     78     }
     79 }
     80 
     81 int dp[maxn];
     82 
     83 int dfs(int u)
     84 {
     85     if (u == en)
     86         return 1LL;
     87     if (dp[u] != -1)
     88     {
     89         return dp[u];
     90     }
     91     int &ans = dp[u];
     92     ans = 0;
     93     for (int i = head[u]; i != -1; i = edge[i].next)
     94     {
     95         int v = edge[i].to;
     96         if (dist[v] < dist[u])
     97         {
     98             ans += dfs(v);
     99         }
    100     }
    101     return ans;
    102 }
    103 
    104 void init()
    105 {
    106     for (int i = 0; i <= n; i++)
    107     {
    108         head[i] = -1;
    109     }
    110     tot = 0;
    111 }
    112 
    113 int main()
    114 {
    115     //ios::sync_with_stdio(false);
    116     //cin.tie(0);
    117     //freopen("input.txt", "r", stdin);
    118     //freopen("output.txt", "w", stdout);
    119     st = 0, en = 1;
    120     while (~scanf("%d", &n) && n)
    121     {
    122         scanf("%d", &m);
    123         init();
    124         int u, v, w;
    125         for (int i = 0; i < m; i++)
    126         {
    127             scanf("%d%d%d", &u, &v, &w);
    128             u--, v--;
    129             AddEdge(u, v, w);
    130             AddEdge(v, u, w);
    131         }
    132         Dijkstra();
    133         memset(dp, -1, sizeof(dp));
    134         printf("%d
    ", dfs(st));
    135     }
    136     return 0;
    137 }
  • 相关阅读:
    百度富文本编辑器的上传图片的路径问题
    laravel初次学习总结及一些细节
    macOS apache配置及开启虚拟服务器的开启,apache开启重写模式
    类似于qq空间类型的评论和回复
    向php提交数据及json
    mac 初次配置apache,及mac下安装mysql
    C#连接mysql数据库插入数据后获取自增长主键ID值
    PHP 真正多线程的使用
    C# 连接mysql数据库
    MySql状态查看方法 MySql如何查看连接数和状态?
  • 原文地址:https://www.cnblogs.com/npugen/p/10758216.html
Copyright © 2011-2022 走看看