Uva 10917 Walk Through the Forest
2012-12-18 20:00
302 查看
完全没考虑原理 瞬间敲出来然后WA了。。。。。坑爹啊
#include<cstdio> #include<cstring> #include<vector> #include<stack> #include<queue> #include<algorithm> using namespace std; typedef long long ll; const int MAXN = 1007; const ll INF = 1LL<<56; struct Dijkstra { struct HeapNode { ll d, u; HeapNode(ll d, int u):d(d), u(u) {} bool operator < (const HeapNode &hn) const { return d > hn.d; } }; vector<int> G[MAXN]; vector<ll> W[MAXN]; bool done[MAXN]; int n; void init(int n) { this->n = n; int i; for(i = 1; i <= n; i++) { G[i].clear(); W[i].clear(); } } void add(int u, int v, ll dist) { G[u].push_back(v); W[u].push_back(dist); } void solve(int s, ll *d) { memset(done, 0, sizeof(done)); int i, u, v, sz; for(i = 1; i <= n; i++) d[i] = INF; priority_queue<HeapNode> pq; d[s] = 0; pq.push(HeapNode(0, s)); while(!pq.empty()) { u = pq.top().u; pq.pop(); if(done[u]) continue; done[u] = true; sz = G[u].size(); for(i = 0; i < sz; i++) { v = G[u][i]; if(d[v] > d[u] + W[u][i]) { d[v] = d[u] + W[u][i]; pq.push(HeapNode(d[v], v)); } } } } }; Dijkstra dijk; ll d[MAXN], dp[MAXN]; vector<int> G[MAXN]; ll getdp(int u) { if(dp[u] != -1) return dp[u]; dp[u] = 0; int i, sz = G[u].size(); for(i = 0; i < sz; i++) dp[u] += getdp(G[u][i]); return dp[u]; } int main() { int n, m; while(~scanf("%d", &n), n) { scanf("%d", &m); int i, j, sz, u, v; ll w; dijk.init(n); for(i = 1; i <= n; i++) G[i].clear(); for(i = 1; i <= m; i++) { scanf("%d%d%lld", &u, &v, &w); dijk.add(u, v, w); dijk.add(v, u, w); } dijk.solve(2, d); for(i = 1; i <= n; i++) { sz = dijk.G[i].size(); for(j = 0; j < sz; j++) { v = dijk.G[i][j]; if(d[i] > d[v]) G[i].push_back(v); } } memset(dp, -1, sizeof(dp)); dp[2] = 1; printf("%lld\n", getdp(1)); } return 0; }
相关文章推荐
- uva 10917 Walk Through the Forest(最短路+DP路径,4级)
- Uva 10917 - Walk Through the Forest 最短路
- UVA - 10917 Walk Through the Forest(最短路的DAG)
- UVA 10917 Walk Through the Forest(dijkstra+DAG上的dp)
- UVA 10917 Walk Through the Forest
- UVA 10917 Walk Through the Forest
- uva 10917 Walk Through the Forest(最短路+DP路径,4级)
- UVa 10917 A Walk Through the Forest
- UVA 10917 - Walk Through the Forest(最短路优化DP)
- UVA 10917(p330)----Walk through the Forest
- UVA10917 Walk Through the Forest (dijkstra + dfs)
- UVA 10917 Walk Through the Forest(Dijkstra+DAG动态规划)
- uva 10917 Walk Through the Forest
- UVa10917 Walk Through the Forest
- UVa 10917 Walk Through the Forest(最短路+DP)
- UVA 10917 - Walk Through the Forest(最短路`dijksta+DP)
- UVa 10917 A Walk through the forest
- UVA 10917 Walk Through the Forest
- UVa10917 A Walk Through the Forest(SPFA+记忆化搜索)
- UVA10917 A Walk Through the Forest