UVA10917 Walk Through the Forest
2018-01-31 19:41
302 查看
题目大意:Jimmy下班后决定每天沿着一条不同的路径回家,欣赏不同的风景。他打算只沿着满足如下条件的(A,B)道路走:存在一条从B出发回家的路,比所有从A出发回家的路径都短。你的任务是计算一共有多少条不同的回家路径。其中公司的编号为1,家的编号为2.
题解:算出每个点到2的最短路,对于每条边(i,j),“存在一条从B出发回家的路,比所有从A出发回家的路径都短”,即d[B] < d[A],说明能从A走到B,于是建新图A->B。不难发现是一个DAG(因为有环就会出现逻辑矛盾)。建反图dp即可
UVA10917
题解:算出每个点到2的最短路,对于每条边(i,j),“存在一条从B出发回家的路,比所有从A出发回家的路径都短”,即d[B] < d[A],说明能从A走到B,于是建新图A->B。不难发现是一个DAG(因为有环就会出现逻辑矛盾)。建反图dp即可
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include <queue> #include <vector> #include <map> #include <string> #include <cmath> #define min(a, b) ((a) < (b) ? (a) : (b)) #define max(a, b) ((a) > (b) ? (a) : (b)) #define abs(a) ((a) < 0 ? (-1 * (a)) : (a)) template<class T> inline void swap(T &a, T &b) { T tmp = a;a = b;b = tmp; } inline void read(int &x) { x = 0;char ch = getchar(), c = ch; while(ch < '0' || ch > '9') c = ch, ch = getchar(); while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar(); if(c == '-') x = -x; } const int INF = 0x3f3f3f3f; const int MAXN = 1000 + 10; struct Edge { int u,v,w,nxt; Edge(int _u, int _v, int _w, int _nxt){u = _u;v = _v;w = _w;nxt = _nxt;} Edge(){} }edge1[MAXN * MAXN], edge2[MAXN * MAXN]; int head1[MAXN], cnt1, head2[MAXN], cnt2; inline void insert1(int a, int b, int c){edge1[++cnt1] = Edge(a,b,c,head1[a]), head1[a] = cnt1;} inline void insert2(int a, int b, int c){edge2[++cnt2] = Edge(a,b,c,head2[a]), head2[a] = cnt2;} struct Node { int u,w; Node(int _u, int _w){u = _u;w = _w;} Node(){} }; struct cmp { bool operator()(Node a, Node b) { return a.w > b.w; } }; std::priority_queue<Node, std::vector<Node>, cmp> q; int n,m,d[MAXN],vis[MAXN],tmp1,tmp2,tmp3,dp[MAXN]; void dij(int S) { memset(d, 0x3f, sizeof(d)), d[S] = 0, memset(vis, 0, sizeof(vis)), q.push(Node(S, 0)); while(q.size()) { Node now = q.top();q.pop(); if(vis[now.u]) continue;vis[now.u] = 1; for(int pos = head1[now.u];pos;pos = edge1[pos].nxt) { int v = edge1[pos].v; if(vis[v]) continue; if(d[v] > d[now.u] + edge1[pos].w) d[v] = d[now.u] + edge1[pos].w, q.push(Node(v, d[v])); } } } int Dp(int x) { if(vis[x]) return dp[x]; for(int pos = head2[x];pos;pos = edge2[pos].nxt) { int v = edge2[pos].v; dp[x] += Dp(v); } vis[x] = 1; return dp[x]; } int main() { while(scanf("%d", &n) != EOF && n) { read(m), memset(head1, 0, sizeof(head1)), memset(head2, 0, sizeof(head2)), cnt1 = 0, cnt2 = 0; for(int i = 1;i <= m;++ i) read(tmp1), read(tmp2), read(tmp3), insert1(tmp1, tmp2, tmp3), insert1(tmp2, tmp1, tmp3); dij(2); for(int i = 1;i <= cnt1;++ i) if(d[edge1[i].v] < d[edge1[i].u]) insert2(edge1[i].v, edge1[i].u, edge1[i].w); memset(vis, 0, sizeof(vis)), memset(dp, 0, sizeof(dp)), vis[1] = 0, dp[1] = 1; printf("%d\n", Dp(2)); } return 0; }
UVA10917
相关文章推荐
- UVA10917 Walk Through the Forest
- UVA10917 Walk Through the Forest
- UVa 10917 A Walk through the forest
- UVA - 10917 Walk Through the Forest (最短路+DP)
- 【UVa】10917 A Walk Through the Forest 最短路+DP
- UVA 10917 Walk Through the Forest(dijkstra+DAG上的dp)
- UVA - 10917 Walk Through the Forest(最短路的DAG)
- Walk Through the Forest UVA - 10917 DAG上的图
- uva 10917 A Walk through the Forest
- UVA 10917 Walk Through the Forest(最短路+dp)
- UVA - 10917 Walk Through the Forest (最短路+DP)
- UVA10917 Walk Through the Forest (dijkstra + dfs)
- UVA10917_Walk Through the Forest
- uva 10917 Walk Through the Forest(最短路)
- UVA10917 A Walk Through the Forest
- UVa10917 Walk Through the Forest
- UVA-10917 Walk Through the Forest (dijkstra+DP)
- uva 10917 - Walk Through the Forest(Dijkstra+Dp)
- UVA 10917 Walk Through the Forest
- uva 10917 - Walk Through the Forest(最短路+记忆化)