UVa10917 Walk Through the Forest
2017-10-19 19:36
316 查看
题目描述 传送门
根据题目意思建立DAG,再统计路径条数。
代码
根据题目意思建立DAG,再统计路径条数。
代码
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<queue> using namespace std; const int maxn=1005,maxm=(maxn-1)*(maxn-2); const int INF=1e9; int a[maxn],d[maxn],h[maxn],cnt,n,m; bool vis[maxn]; struct Edge{ int t,d,nxt; Edge(int a=0,int b=0,int c=0):t(a),d(b),nxt(c){} }edges[maxm*2]; struct heap{ int d,v; heap(int a=0,int b=0):d(a),v(b){} bool operator<(const heap&a)const{ return d>a.d; } }; void addedge(int from,int to,int dist){ edges[++cnt]=Edge(to,dist,h[from]); h[from]=cnt; edges[++cnt]=Edge(from,dist,h[to]); h[to]=cnt; } int dfs(int u){ if(a[u]>-1) return a[u]; a[u]=0; for(int i=h[u];i;i=edges[i].nxt){ int v=edges[i].t; if(d[u]<d[v]) a[u]+=dfs(v); } return a[u]; } int main(){ while(scanf("%d",&n)==1&&n){ scanf("%d",&m); cnt=0; memset(h,0,sizeof(h)); for(int i=0;i<m;i++){ int a,b,c; scanf("%d%d%d",&a,&b,&c); addedge(a,b,c); } priority_queue<heap> q; q.push(heap(0,2)); for(int i=1;i<=n;i++) d[i]=INF; d[2]=0; memset(vis,0,sizeof(vis)); while(!q.empty()){ heap x=q.top();q.pop(); int u=x.v; if(vis[u]) continue; vis[u]=1; for(int i=h[u];i;i=edges[i].nxt){ int v=edges[i].t; if(d[u]+edges[i].d<d[v]){ d[v]=d[u]+edges[i].d; q.push(heap(d[v],v)); } } } memset(a,-1,sizeof(a)); memset(vis,0,sizeof(vis)); a[1]=1; printf("%d\n",dfs(2)); } return 0; }
相关文章推荐
- Uva 10917 - Walk Through the Forest 最短路
- Uva 10917 Walk Through the Forest(DAG拓扑排序)
- UVa10917 - Walk Through the Forest(单源最短路径及动态规划)
- UVa 10917 A Walk Through the Forest
- UVA 10917 Walk Through the Forest(Dijkstra+DAG动态规划)
- uva 10917 Walk Through the Forest
- UVA 10917 - Walk Through the Forest(最短路优化DP)
- UVA10917 A Walk Through the Forest
- UVA 10917(p330)----Walk through the Forest
- UVa 10917 Walk Through the Forest(最短路+DP)
- UVA 10917 - Walk Through the Forest(最短路`dijksta+DP)
- UVA 10917 Walk Through the Forest(最短路+dp)
- Uva 10917 Walk Through the Forest
- 【UVa】10917 A Walk Through the Forest 最短路+DP
- UVA 10917 - Walk Through the Forest(dijstra)
- UVa 10917 - Walk Through the Forest(Dijkstra + DFS)
- UVa10917 A Walk Through the Forest(SPFA+记忆化搜索)
- UVA - 10917 Walk Through the Forest(最短路的DAG)
- 【uva10917】Walk Through the Forest (最短路)
- uva 10917 - Walk Through the Forest(Dijkstra+Dp)