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
Sample Output
2
4
题解:满足条件的道路<A, B>其实就是满足式子d[B] < d[A],因此跑一边最短路之后,可行路径就出来了,显然只保留可行路径的图是DAG,有向是肯定的,无环也很好理解,对于环上的节点,按照顺时针(或者逆时针)的顺序始终满足上述不等式,绕一圈之后会出现d[s] < d[s],这样的矛盾不等式,所以无环,DAG上统计路径就很简单了,记忆化搜索呗。
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 1; i <= (n); i++)
#define sqr(x) ((x) * (x)) const int maxn = + ;
const int maxm = + ;
const int maxs = + ; typedef long long LL;
typedef pair<int, int> pii;
typedef pair<double, double> pdd; const LL unit = 1LL;
const int INF = 0x3f3f3f3f;
const LL mod = ;
const double eps = 1e-;
const double inf = 1e15;
const double pi = acos(-1.0); struct Edge
{
int to, next, w;
} edge[maxm]; struct HeapNode
{
int dis, u;
bool operator<(const HeapNode &a) const
{
return dis > a.dis;
}
}; int tot, head[maxn]; void AddEdge(int u, int v, int w)
{
edge[tot].to = v;
edge[tot].next = head[u];
edge[tot].w = w;
head[u] = tot++;
} int st, en, n, m;
int dist[maxn];
bool vis[maxn]; void Dijkstra()
{
for (int i = ; i <= n; i++)
{
dist[i] = INF;
vis[i] = false;
}
dist[en] = ;
priority_queue<HeapNode> que;
que.push((HeapNode){, en});
while (!que.empty())
{
HeapNode x = que.top();
que.pop();
if (vis[x.u])
continue;
int u = x.u;
vis[u] = true;
for (int i = head[u]; i != -; i = edge[i].next)
{
int v = edge[i].to;
if (dist[v] > dist[u] + edge[i].w)
{
dist[v] = dist[u] + edge[i].w;
que.push((HeapNode){dist[v], v});
}
}
}
} int dp[maxn]; int dfs(int u)
{
if (u == en)
return 1LL;
if (dp[u] != -)
{
return dp[u];
}
int &ans = dp[u];
ans = ;
for (int i = head[u]; i != -; i = edge[i].next)
{
int v = edge[i].to;
if (dist[v] < dist[u])
{
ans += dfs(v);
}
}
return ans;
} void init()
{
for (int i = ; i <= n; i++)
{
head[i] = -;
}
tot = ;
} int main()
{
//ios::sync_with_stdio(false);
//cin.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
st = , en = ;
while (~scanf("%d", &n) && n)
{
scanf("%d", &m);
init();
int u, v, w;
for (int i = ; i < m; i++)
{
scanf("%d%d%d", &u, &v, &w);
u--, v--;
AddEdge(u, v, w);
AddEdge(v, u, w);
}
Dijkstra();
memset(dp, -, sizeof(dp));
printf("%d\n", dfs(st));
}
return ;
}