标签:des style blog class c code
Time Limit: 2000/1000 MS
(Java/Others) Memory Limit: 32768/32768 K
(Java/Others)
Total Submission(s): 1010 Accepted
Submission(s): 332
题意:
给出一个有向图,求其次短路径数。
最短路径:
先求次短路径:
从源点到汇点构图求出所有ds[i];
从汇点到源点构图求出所有de[i]; ds[i]、de[i]分别为源点到其他点最短路长,汇点到其他点最短路长
然后枚举所有边,当枚举到的边(i,j)长度ds[i]+de[j]+(i,j)仅仅大于源点到汇点的最短路长时其为次短路径
求出次短路长后dfs求其数量,犯了点错误TLE了好多次.不过dfs的耗时有点大
代码:
1 //453MS 256K 2508 B C++ 2 #include<iostream> 3 #include<queue> 4 #include<vector> 5 #define inf 0x7ffffff 6 #define N 55 7 using namespace std; 8 struct node{ 9 int v,d; 10 node(int a,int b){ 11 v=a;d=b; 12 } 13 }; 14 vector<node>Vs[N],Ve[N],V[N]; 15 int vis[N],dd; 16 int n,cnt,s,e; 17 void spfa_s(int u,int d[]) 18 { 19 memset(vis,0,sizeof(vis)); 20 for(int i=0;i<n;i++) 21 d[i]=inf; 22 d[u]=0; 23 vis[u]=1; 24 queue<int>Q; 25 Q.push(u); 26 while(!Q.empty()){ 27 u=Q.front(); 28 Q.pop(); 29 vis[u]=0; 30 int m=Vs[u].size(); 31 for(int i=0;i<m;i++){ 32 int v=Vs[u][i].v; 33 int w=Vs[u][i].d; 34 if(d[v]>d[u]+w){ 35 d[v]=d[u]+w; 36 if(!vis[v]){ 37 vis[v]=1; 38 Q.push(v); 39 } 40 } 41 } 42 } 43 } 44 void spfa_e(int u,int d[]) 45 { 46 memset(vis,0,sizeof(vis)); 47 for(int i=0;i<n;i++) 48 d[i]=inf; 49 d[u]=0; 50 vis[u]=1; 51 queue<int>Q; 52 Q.push(u); 53 while(!Q.empty()){ 54 u=Q.front(); 55 Q.pop(); 56 vis[u]=0; 57 int m=Ve[u].size(); 58 for(int i=0;i<m;i++){ 59 int v=Ve[u][i].v; 60 int w=Ve[u][i].d; 61 if(d[v]>d[u]+w){ 62 d[v]=d[u]+w; 63 if(!vis[v]){ 64 vis[v]=1; 65 Q.push(v); 66 } 67 } 68 } 69 } 70 } 71 void dfs(int u,int w) 72 { 73 if(u==e && w==dd) cnt++; 74 if(u==e || w>=dd) return; 75 int m=Vs[u].size(); 76 for(int i=0;i<m;i++){ 77 int v=Vs[u][i].v; 78 int tw=Vs[u][i].d; 79 if(!vis[v]){ 80 vis[v]=1; 81 dfs(v,tw+w); 82 vis[v]=0; 83 } 84 } 85 } 86 int main(void) 87 { 88 int ds[N],de[N]; 89 int m; 90 int u,v,w; 91 while(scanf("%d%d%d%d",&n,&m,&s,&e)!=EOF) 92 { 93 for(int i=0;i<n;i++){ 94 Vs[i].clear(); 95 Ve[i].clear(); 96 } 97 for(int i=0;i<m;i++){ 98 scanf("%d%d%d",&u,&v,&w); 99 Vs[u].push_back(node(v,w)); 100 Ve[v].push_back(node(u,w)); 101 } 102 spfa_s(s,ds); 103 spfa_e(e,de); 104 dd=inf; 105 for(int i=0;i<n;i++) 106 for(int j=0;j<Vs[i].size();j++){ 107 int td=ds[i]+de[Vs[i][j].v]+Vs[i][j].d; 108 if(td!=ds[e] && td<dd) dd=td; 109 } 110 cnt=0; 111 memset(vis,0,sizeof(vis)); 112 dfs(s,0); 113 printf("%d %d\n",dd,cnt); 114 } 115 return 0; 116 }
==========================================================================================================================================================================================================
1 //15MS 256K 2291 B C++ 2 #include<iostream> 3 #include<queue> 4 #include<vector> 5 #define inf 0x7ffffff 6 #define N 55 7 using namespace std; 8 struct edge{ 9 int v,w; 10 edge(int a,int b){ 11 v=a;w=b; 12 } 13 }; 14 struct node{ 15 int v,w; 16 int mark; 17 bool operator < (const node &p) const{ 18 if(p.w!=w) return p.w<w; 19 return p.v<v; 20 } 21 }; 22 vector<edge>V[N]; 23 int n,m,s,e; 24 int d[N][3]; //记录最短路和次短路距离 25 int dp[N][3]; //记录最短路和次短路条数 26 int vis[N][3]; 27 void dijkstra(int s) //优先队列实现dij 28 { 29 for(int i=0;i<n;i++) 30 d[i][1]=d[i][2]=inf; 31 memset(dp,0,sizeof(dp)); 32 memset(vis,0,sizeof(vis)); 33 priority_queue<node>Q; 34 node p,q; 35 d[s][1]=0; 36 dp[s][1]=1; 37 p.w=0,p.mark=1,p.v=s; 38 Q.push(p); 39 while(!Q.empty()){ 40 p=Q.top(); 41 Q.pop(); 42 if(vis[p.v][p.mark]) continue; 43 vis[p.v][p.mark]=1; 44 for(int i=0;i<V[p.v].size();i++){ 45 int v=V[p.v][i].v; 46 int w=V[p.v][i].w; 47 if(!vis[v][1] && d[v][1]>p.w+w){ //更新最短路长 48 if(d[v][1]!=inf){ //原最短路可为次短路 49 d[v][2]=d[v][1]; 50 dp[v][2]=dp[v][1]; 51 q.v=v,q.w=d[v][2],q.mark=2; 52 Q.push(q); 53 } 54 d[v][1]=p.w+w; 55 dp[v][1]=dp[p.v][p.mark]; 56 q.v=v,q.w=d[v][1],q.mark=1; 57 Q.push(q); 58 }else if(!vis[v][1] && d[v][1]==p.w+w){ //更新最短路长数 59 dp[v][1]+=dp[p.v][p.mark]; 60 }else if(!vis[v][2] && d[v][2]>p.w+w){ //更新次短路长 61 d[v][2]=p.w+w; 62 dp[v][2]=dp[p.v][p.mark]; 63 q.w=d[v][2],q.v=v,q.mark=2; 64 Q.push(q); 65 }else if(!vis[v][2] && d[v][2]==p.w+w){ //更新次短路长数 66 dp[v][2]+=dp[p.v][p.mark]; 67 } 68 } 69 } 70 } 71 int main(void) 72 { 73 int u,v,w; 74 while(scanf("%d%d%d%d",&n,&m,&s,&e)!=EOF) 75 { 76 for(int i=0;i<n;i++) 77 V[i].clear(); 78 for(int i=0;i<m;i++){ 79 scanf("%d%d%d",&u,&v,&w); 80 V[u].push_back(edge(v,w)); 81 } 82 dijkstra(s); 83 printf("%d %d\n",d[e][2],dp[e][2]); 84 } 85 return 0; 86 }
hdu 3191 How Many Paths Are There (次短路径数),布布扣,bubuko.com
hdu 3191 How Many Paths Are There (次短路径数)
标签:des style blog class c code
原文地址:http://www.cnblogs.com/GO-NO-1/p/3738165.html