hdu 3191 How Many Paths Are There (次短路径数)

时间:2022-08-06 15:25:26

How Many Paths Are There

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1010    Accepted Submission(s): 332

Problem Description
  oooccc1 is a Software Engineer who has to ride to the work place every Monday through Friday. For a long period, he went to office with the shortest path because he loves to sleep late…Time goes by, he find that he should have some changes as you could see, always riding with the same path is boring.
  One day, oooccc1 got an idea! Why could I take another path? Tired at all the tasks he got, he got no time to carry it out. As a best friend of his, you’re going to help him!
  Since oooccc1 is now getting up earlier, he is glad to take those paths, which are a little longer than the shortest one. To be precisely, you are going to find all the second shortest paths.
  You would be given a directed graph G, together with the start point S which stands for oooccc’1 his house and target point E presents his office. And there is no cycle in the graph. Your task is to tell him how long are these paths and how many there are.
 
Input
There are some cases. Proceed till the end of file.
The first line of each case is three integers N, M, S, E (3 <= N <= 50, 0 <= S , E <N)
N stands for the nodes in that graph, M stands for the number of edges, S stands for the start point, and E stands for the end point.
Then M lines follows to describe the edges: x y w. x stands for the start point, and y stands for another point, w stands for the length between x and y. 
All the nodes are marked from 0 to N-1.
 
Output
For each case,please output the length and count for those second shortest paths in one line. Separate them with a single space.
 
Sample Input
3 3 0 2
0 2 5
0 1 4
1 2 2
 
Sample Output
6 1
 
Author
ZSTU
 
Source
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  2363 2377 2433 2833 1688 
 

题意:

给出一个有向图,求其次短路径数。

最短路径:

先求次短路径:

从源点到汇点构图求出所有ds[i];

从汇点到源点构图求出所有de[i]; ds[i]、de[i]分别为源点到其他点最短路长,汇点到其他点最短路长

然后枚举所有边,当枚举到的边(i,j)长度ds[i]+de[j]+(i,j)仅仅大于源点到汇点的最短路长时其为次短路径

求出次短路长后dfs求其数量,犯了点错误TLE了好多次.不过dfs的耗时有点大

代码:

 //453MS    256K    2508 B    C++
#include<iostream>
#include<queue>
#include<vector>
#define inf 0x7ffffff
#define N 55
using namespace std;
struct node{
int v,d;
node(int a,int b){
v=a;d=b;
}
};
vector<node>Vs[N],Ve[N],V[N];
int vis[N],dd;
int n,cnt,s,e;
void spfa_s(int u,int d[])
{
memset(vis,,sizeof(vis));
for(int i=;i<n;i++)
d[i]=inf;
d[u]=;
vis[u]=;
queue<int>Q;
Q.push(u);
while(!Q.empty()){
u=Q.front();
Q.pop();
vis[u]=;
int m=Vs[u].size();
for(int i=;i<m;i++){
int v=Vs[u][i].v;
int w=Vs[u][i].d;
if(d[v]>d[u]+w){
d[v]=d[u]+w;
if(!vis[v]){
vis[v]=;
Q.push(v);
}
}
}
}
}
void spfa_e(int u,int d[])
{
memset(vis,,sizeof(vis));
for(int i=;i<n;i++)
d[i]=inf;
d[u]=;
vis[u]=;
queue<int>Q;
Q.push(u);
while(!Q.empty()){
u=Q.front();
Q.pop();
vis[u]=;
int m=Ve[u].size();
for(int i=;i<m;i++){
int v=Ve[u][i].v;
int w=Ve[u][i].d;
if(d[v]>d[u]+w){
d[v]=d[u]+w;
if(!vis[v]){
vis[v]=;
Q.push(v);
}
}
}
}
}
void dfs(int u,int w)
{
if(u==e && w==dd) cnt++;
if(u==e || w>=dd) return;
int m=Vs[u].size();
for(int i=;i<m;i++){
int v=Vs[u][i].v;
int tw=Vs[u][i].d;
if(!vis[v]){
vis[v]=;
dfs(v,tw+w);
vis[v]=;
}
}
}
int main(void)
{
int ds[N],de[N];
int m;
int u,v,w;
while(scanf("%d%d%d%d",&n,&m,&s,&e)!=EOF)
{
for(int i=;i<n;i++){
Vs[i].clear();
Ve[i].clear();
}
for(int i=;i<m;i++){
scanf("%d%d%d",&u,&v,&w);
Vs[u].push_back(node(v,w));
Ve[v].push_back(node(u,w));
}
spfa_s(s,ds);
spfa_e(e,de);
dd=inf;
for(int i=;i<n;i++)
for(int j=;j<Vs[i].size();j++){
int td=ds[i]+de[Vs[i][j].v]+Vs[i][j].d;
if(td!=ds[e] && td<dd) dd=td;
}
cnt=;
memset(vis,,sizeof(vis));
dfs(s,);
printf("%d %d\n",dd,cnt);
}
return ;
}

==========================================================================================================================================================================================================

发现有次短路模板,时间复杂度比我的好多了.!

 //15MS    256K    2291 B    C++
#include<iostream>
#include<queue>
#include<vector>
#define inf 0x7ffffff
#define N 55
using namespace std;
struct edge{
int v,w;
edge(int a,int b){
v=a;w=b;
}
};
struct node{
int v,w;
int mark;
bool operator < (const node &p) const{
if(p.w!=w) return p.w<w;
return p.v<v;
}
};
vector<edge>V[N];
int n,m,s,e;
int d[N][]; //记录最短路和次短路距离
int dp[N][]; //记录最短路和次短路条数
int vis[N][];
void dijkstra(int s) //优先队列实现dij
{
for(int i=;i<n;i++)
d[i][]=d[i][]=inf;
memset(dp,,sizeof(dp));
memset(vis,,sizeof(vis));
priority_queue<node>Q;
node p,q;
d[s][]=;
dp[s][]=;
p.w=,p.mark=,p.v=s;
Q.push(p);
while(!Q.empty()){
p=Q.top();
Q.pop();
if(vis[p.v][p.mark]) continue;
vis[p.v][p.mark]=;
for(int i=;i<V[p.v].size();i++){
int v=V[p.v][i].v;
int w=V[p.v][i].w;
if(!vis[v][] && d[v][]>p.w+w){ //更新最短路长
if(d[v][]!=inf){ //原最短路可为次短路
d[v][]=d[v][];
dp[v][]=dp[v][];
q.v=v,q.w=d[v][],q.mark=;
Q.push(q);
}
d[v][]=p.w+w;
dp[v][]=dp[p.v][p.mark];
q.v=v,q.w=d[v][],q.mark=;
Q.push(q);
}else if(!vis[v][] && d[v][]==p.w+w){ //更新最短路长数
dp[v][]+=dp[p.v][p.mark];
}else if(!vis[v][] && d[v][]>p.w+w){ //更新次短路长
d[v][]=p.w+w;
dp[v][]=dp[p.v][p.mark];
q.w=d[v][],q.v=v,q.mark=;
Q.push(q);
}else if(!vis[v][] && d[v][]==p.w+w){ //更新次短路长数
dp[v][]+=dp[p.v][p.mark];
}
}
}
}
int main(void)
{
int u,v,w;
while(scanf("%d%d%d%d",&n,&m,&s,&e)!=EOF)
{
for(int i=;i<n;i++)
V[i].clear();
for(int i=;i<m;i++){
scanf("%d%d%d",&u,&v,&w);
V[u].push_back(edge(v,w));
}
dijkstra(s);
printf("%d %d\n",d[e][],dp[e][]);
}
return ;
}