poj3463 hdu1688 次短路+优先队列 如果让我说:我只能说,实力决定一切
来源:互联网 发布:淘宝国庆节活动 编辑:程序博客网 时间:2024/04/30 09:09
思路:该题的思路和hdu3191一样,只是题目要求输出最短路和次短路的条数 最短路和次短路要求相差1
#include<iostream>#include<algorithm>#include<cstdio>#include<string.h>#include<vector>#include<queue>using namespace std;const int inf=1<<30;const int maxn=1010;struct Edge{ int to,w;};struct node{ int v,dist; int mark; bool friend operator <(const node &a,const node &b) { if(a.dist!=b.dist) return a.dist>b.dist; return a.v>b.v; }};priority_queue<node> Q;vector<Edge>gra[maxn*10];int dp[maxn][3];int vis[maxn][3];int dis[maxn][3];int T,N,M,A,B,L;void init(){ for(int i=0; i<maxn; i++) { dp[i][1]=dp[i][2]=0; vis[i][1]=vis[i][2]=0; dis[i][1]=dis[i][2]=inf; }}void Dijstra(int st,int ed){ dis[st][1]=0; dp[st][1]=1; node q,p; p.dist=0; p.mark=1; p.v=st; Q.push(p); while(!Q.empty()) { q=Q.top(); Q.pop(); if(vis[q.v][q.mark]) continue; vis[q.v][q.mark]=1; for(int i=0; i<gra[q.v].size(); i++) { int vv=gra[q.v][i].to; int ww=gra[q.v][i].w; if(!vis[vv][1]&&dis[vv][1]>q.dist+ww) { if(dis[vv][1]!=inf) { dis[vv][2]=dis[vv][1]; dp[vv][2]=dp[vv][1]; p.dist=dis[vv][2]; p.mark=2; p.v=vv; Q.push(p); } dis[vv][1]=q.dist+ww; dp[vv][1]=dp[q.v][q.mark]; p.mark=1; p.dist=dis[vv][1]; p.v=vv; Q.push(p); } else if(!vis[vv][1]&&dis[vv][1]==q.dist+ww) { dp[vv][1]+=dp[q.v][q.mark]; } else if(!vis[vv][2]&&dis[vv][2]>q.dist+ww) { dis[vv][2]=q.dist+ww; dp[vv][2]=dp[q.v][q.mark]; p.mark=2; p.dist=dis[vv][2]; p.v=vv; Q.push(p); } else if(!vis[vv][2]&&dis[vv][2]==q.dist+ww) { dp[vv][2]+=dp[q.v][q.mark]; } } }}int main(){ scanf("%d",&T); Edge tem; int st,ed; while(T--) { init(); for(int i=0;i<maxn*10;i++) gra[i].clear(); scanf("%d%d",&N,&M); for(int i=0; i<M; i++) { scanf("%d%d%d",&A,&B,&L); tem.to=B; tem.w=L; gra[A].push_back(tem); } scanf("%d%d",&st,&ed); Dijstra(st,ed); if(dis[ed][1]+1==dis[ed][2]) printf("%d\n",dp[ed][1]+dp[ed][2]); else printf("%d\n",dp[ed][1]); } return 0;}
- poj3463 hdu1688 次短路+优先队列 如果让我说:我只能说,实力决定一切
- hdu 3191 次短路+优先队列 如果让我说:我只能说,实力决定一切
- hdu1301 MST+优先队列 如果让我说:我只能说,实力决定一切。
- hdu 1233 kruskal+优先队列 如果让我说:我只能说,实力决定一切。
- poj3067 树状数组+优先队列 如果让我说:我只能说,实力代表一切
- hdu 2642 二维数组+判重 如果让我说:我只能说,实力决定一切
- hdu2767强连通+缩点 如果让我说:我只能说,实力决定一切
- hdu1269强连通水题 如果让我说:我只能说,实力决定一切
- poj2553强连通+缩点 如果让我说:我只能说,实力决定一切
- poj2186 强连通+缩点 如果让我说:我只能说,实力决定一切
- hdu2544 spfa 第一弹 如果让我说:我只能说,实力决定一切
- hdu1535 spfa() 如果让我说:我只能说,实力决定一切
- hdu2112 spfa() 如果让我说:我只能说,实力决定一切
- hdu3339 spfa()+dp 如果让我说:我只能说,实力决定一切
- hdu 3336 KMP走起 如果让我说:我只能说,实力决定一切
- hdu3746 KMP之next[]威武 如果让我说:我只能说,实力决定一切。
- poj 2752 KMP+DFS() 如果让我说:我只能说,实力决定一切
- poj3974 hdu3068最长回文子串 如果让我说:我只能说,实力决定一切
- 【eclipse】入门级问题Syntax error on token ""
- OpenGL的作用机制
- android开发问题记录(2013/4/23)
- UIImage
- 虚函数深入挖掘-构造函数与虚函数【1】
- poj3463 hdu1688 次短路+优先队列 如果让我说:我只能说,实力决定一切
- 栈和堆的区别
- Android应用实例之---使用Linkify + 正则式区分微博文本链接及跳转处理
- Asp.Net4.0/VS2010新变化(3):webform中也可以直接url路由
- Android中采用html页面布局以及调用JavaScript
- sql sum 函数 统计计算值 不正确?相对于计算器计算的值少了一点点
- UvaLive-4108-SKYLINE
- glMatrixMode()函数的理解,以及3d2d视图转换
- Eclipse插件EasyExplore 打开文件所在目录