UVa 10917 Walk Through the Forest(最短路+DP)
2015-03-30 22:34
260 查看
求出办公室到每一点的最短路d[i],然后记忆话搜索,dp[i]由和i点相邻且d[j]<d[i]的点更新,是树形DP
代码:
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <vector> #define INF 10000000 using namespace std; int N,M; struct edge{ int s,t,c; }; vector <edge> E; vector <int> G[1005]; int d[1005]; bool vis[1005]; int p[1005]; int dp[1005]; int solve(int n){ if(dp ) return dp ; for(int i=0;i<G .size();i++){ edge &tmp=E[G [i]]; if(d[tmp.t]<d ){ dp +=solve(tmp.t); } } return dp ; } void addedge(int s,int t,int cost){ E.push_back(edge{s,t,cost}); G[s].push_back(E.size()-1); } struct HNode{ int d,u; bool operator <(const HNode &obj)const{ return d>obj.d; } }; void Dij(int s){ for(int i=1;i<=N;i++){ d[i]=INF; } d[s]=0; memset(vis,0,sizeof(vis)); priority_queue<HNode> Q; Q.push(HNode{0,s}); while(!Q.empty()){ int u=Q.top().u; Q.pop(); if(vis[u]) continue; vis[u]=1; for(int i=0;i<G[u].size();i++){ edge &tmp=E[G[u][i]]; if(d[tmp.t]>d[u]+tmp.c){ d[tmp.t]=d[u]+tmp.c; p[tmp.t]=u; Q.push(HNode{d[tmp.t],tmp.t}); } } } } int main(){ while(~scanf("%d",&N)){ for(int i=1;i<=N;i++){ G[i].clear(); } E.clear(); if(!N) break; scanf("%d",&M); for(int i=0;i<M;i++){ int s,t,c; scanf("%d%d%d",&s,&t,&c); addedge(s,t,c); addedge(t,s,c); } Dij(2); memset(dp,0,sizeof(dp)); dp[2]=1; solve(1); for(int i=1;i<=N;i++){ //cout<<i<<' '<<dp[i]<<endl; } printf("%d\n",solve(1)); } return 0; }
相关文章推荐
- uva 10917 Walk Through the Forest(最短路+DP路径,4级)
- UVA 10917 - Walk Through the Forest(最短路`dijksta+DP)
- UVA 10917 Walk Through the Forest(最短路+dp)
- UVA - 10917 Walk Through the Forest (最短路+DP)
- UVA 10917 - Walk Through the Forest(最短路优化DP)
- UVA - 10917 Walk Through the Forest (最短路+DP)
- uva 10917 Walk Through the Forest(最短路+DP路径,4级)
- uva 10917 - Walk Through the Forest(Dijkstra+Dp)
- 【uva10917】Walk Through the Forest (最短路)
- Uva 10917 - Walk Through the Forest 最短路
- UVa10917 - Walk Through the Forest(dijkstra+dp)
- UVA 10917 Walk Through the Forest(dijkstra+DAG上的dp)
- uva 10917 - Walk Through the Forest(最短路+记忆化)
- UVA - 10917 Walk Through the Forest(最短路的DAG)
- Uva 10917 - Walk Through the Forest 最短路
- uva 10917 Walk Through the Forest(最短路)
- UVA-10917 Walk Through the Forest (dijkstra+DP)
- HDU - 1142 A Walk Through the Forest (DP + 最短路)
- UVA10917 A Walk Through the Forest
- uva 10917 A Walk through the Forest