hdu 3191 How Many Paths Are There
2014-05-06 18:58
393 查看
http://acm.hdu.edu.cn/showproblem.php?pid=3191
这道题求次短路经和路径数
View Code
这道题求次短路经和路径数
#include <cstdio> #include <cstring> #include <vector> #include <queue> #include <algorithm> #define maxn 2000 using namespace std; const int inf=1<<30; struct edge { int v,w; }; struct node { int d,v; int mark; bool operator < (const node &a)const { if(d!=a.d) return d>a.d; return v>a.v; } }; vector<edge>edges[maxn]; int dis[maxn][3]; int vis[maxn][3]; int path[maxn][3]; int n,m,a,b,c,s1,f; node st; void inti() { for(int i=0; i<=n; i++) { dis[i][1]=dis[i][2]=inf; } memset(path,0,sizeof(path)); memset(vis,false,sizeof(vis)); } void dijkstra(int s,int e) { inti(); priority_queue<node>q; dis[s][1]=0; path[s][1]=1; memset(vis,false,sizeof(vis)); st.d=0; st.v=s; st.mark=1; q.push(st); while(!q.empty()) { node st1=q.top(); q.pop(); if(vis[st1.v][st1.mark]) continue; vis[st1.v][st1.mark]=true; for(int i=0; i<(int)edges[st1.v].size(); i++) { int v1=edges[st1.v][i].v; int w1=edges[st1.v][i].w; if(!vis[v1][1]&&st1.d+w1<dis[v1][1]) { if(dis[v1][1]!=inf) { dis[v1][2]=dis[v1][1]; path[v1][2]=path[v1][1]; st.d=dis[v1][2]; st.v=v1; st.mark=2; q.push(st); } dis[v1][1]=st1.d+w1; path[v1][1]=path[st1.v][st1.mark]; st.v=v1; st.mark=1; st.d=dis[v1][1]; q.push(st); } else if(!vis[v1][1]&&st1.d+w1==dis[v1][1]) { path[v1][1]+=path[st1.v][st1.mark]; } else if(!vis[v1][2]&&st1.d+w1<dis[v1][2]) { dis[v1][2]=st1.d+w1; path[v1][2]=path[st1.v][st1.mark]; st.d=dis[v1][2]; st.v=v1; st.mark=2; q.push(st); } else if(!vis[v1][2]&&st1.d+w1==dis[v1][2]) { path[v1][2]+=path[st1.v][st1.mark]; } } } } int main() { while(scanf("%d%d%d%d",&n,&m,&s1,&f)!=EOF) { inti(); for(int i=0; i<=n; i++) edges[i].clear(); for(int i=0; i<m; i++) { scanf("%d%d%d",&a,&b,&c); edge m1; m1.v=b; m1.w=c; edges[a].push_back(m1); } dijkstra(s1,f); printf("%d %d\n",dis[f][2],path[f][2]); } return 0; }
View Code
相关文章推荐
- 【HDU】3191 How Many Paths Are There 次短路计数
- hdu 3191 How Many Paths Are There(次短路+条数)
- hdu 3191 How Many Paths Are There(次短路) 很纠结题目。。。
- HDU-3191 How Many Paths Are There(有向图次短路条数)
- HDU 3191 How Many Paths Are There 【次短路计数】
- HDU 3191 How Many Paths Are There
- hdu 3191 How Many Paths Are There (次短路径数)
- HDU 3191 How Many Paths Are There
- HDU 3191 How Many Paths Are There
- HDU 3191 次短路 How Many Paths Are There
- HDU_3191_How Many Paths Are There(次短路条数)
- HDU-3191-How Many Paths Are There
- hdu 1688 Sightseeing (次短路及次短路数)&&pku3255 Roadblocks &&3191 How Many Paths Are There
- hdu 3191 How Many Paths Are There(求次短路径和次短路径条数)
- HDU 3191 How Many Paths Are There(SPFA)
- HDU-3191 How Many Paths Are There 次最短路
- HDU 3139 How Many Paths Are There
- hdu6181 How Many Paths Are There(次短路条数[模板])
- HDU3191-How many paths are there(次短路的长度及其个数)
- hdu3191 How Many Paths Are There