• 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 }
  • 相关阅读:
    Oracle将放弃prometric
    07年博客迁移:回记Oracle的三天培训
    iptv速率实测
    Oracle database 11g r2最新安装体验
    Oracle中dblink所产生远程会话的一些表现
    07年博客迁移:Home desktop migrate to fedora
    Mysql:语法:字符集、排序规则
    Mysql:事务管理——未完待续
    Mysql:SQL语句:用户、权限、信息、状态、设置、复制、会话、prepare sql 等
    Mysql:函数、操作符
  • 原文地址:https://www.cnblogs.com/npugen/p/10758216.html
Copyright © 2020-2023  润新知