UVA
来源:互联网 发布:程序员的一天小品 编辑:程序博客网 时间:2024/05/21 17:09
题意:Jimmy最近打算穿过一个森林,但是他比较傲娇,于是他决定只走一些特殊的道路,他打算只沿着满足如下条件的(A,B)道路走:存在一条从B出发回家的路,比所有从A出发回家的路径都短。你的任务是计算一共有多少条不同的回家路径。其中起点的编号为1,终点的编号为2.
分析:把最短路算出来,迎最短路DAG走,注意记忆搜索DP!
#include<iostream>#include<stdio.h>#include<algorithm>#include<string.h>#include<queue>#include<vector>const int maxn = 100000;const int inf = 1e9;typedef long long ll;using namespace std;struct HeapNode{ int d,u; bool operator < (const HeapNode &rhs) const { return d > rhs.d; }};struct Edge{ int fr,to,dist,nex;};struct Dijkstra{ int n,m,cnt; Edge edges[maxn*2+5]; bool done[maxn+5]; int d[maxn+5],p[maxn+5],head[maxn+5],f[maxn+5]; void init(int n) { this->n = n, m = 0, cnt = 0; for(int i=0; i<=n; i++) head[i] = -1,f[i]=0; f[2] = 1; } void addedge(int fr,int to,int dist) { edges[++m] = (Edge) { fr,to,dist,head[fr] }; head[fr] = m; } void dijkstra(int s) { priority_queue<HeapNode>Q; for(int i=0; i<=n; i++) d[i] = inf; d[s] = 0, p[s] =1; memset(done,0,sizeof(done)); Q.push((HeapNode) { 0,s }); while(!Q.empty()) { HeapNode x = Q.top(); Q.pop(); int u = x.u; if(done[u]) continue; done[u] = true; for(int i=head[u]; i!=-1 ;) { Edge& e = edges[i]; i = e.nex; if(d[e.to]>=d[u]+e.dist) { if(d[e.to]>d[u]+e.dist) { d[e.to] = d[u] + e.dist; Q.push((HeapNode) { d[e.to],e.to }); } } } } } void dfs(int u) { if(f[u]) return; f[u] = 0; for(int i=head[u]; i!=-1 ;) { Edge& e = edges[i]; i = e.nex; if(d[e.to]<d[u]) dfs(e.to), f[u]+=f[e.to]; } }} my;int n,m,cnt,p[maxn+5];int main(){ while(scanf("%d",&n)&&n) { scanf("%d",&m); my.init(n); for(int i=1; i<=m; i++) { int x,y,w; scanf("%d %d %d",&x,&y,&w); my.addedge(x,y,w), my.addedge(y,x,w); } my.dijkstra(2); my.dfs(1); printf("%d\n",my.f[1]); } return 0;}
阅读全文
0 0
- uva
- UVA
- UVA
- UVA
- uva
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- 拦截导弹
- nodejs发起HTTPS,GET,POST请求Face++人脸识别
- 大三狗的日常总结与反思
- QSS应用
- 关于hibernate如何把原生sql查出的结果转化为对象
- UVA
- bashrc for cadence
- Standard C/C++ on Windows
- Mac下切换到Root用户
- BZOJ4260 Codechef REBXOR trie
- 大学第一堂课程”打印helloworld“
- 10.12
- poj 1067 取石子游戏(威佐夫博弈)
- 天弘基金移动App客户端架构优化之路