How Many Paths Are There
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 866 Accepted Submission(s): 274
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.
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.
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
题意:求一个有向图的起点到终点的次短路径的长度和条数。
思路:dij+heap。设dis[i][0]为到i的最短路径,dis[i][1]为到i的次短路径,cnt[i][0]为到i的最短路径条数,cnt[i][1]为到i的次短路径条数。若找到一个点x,有四种情况:
(1)dis[x][0]+w(x,v)<dis[v][0],若本来的dis[v][0]存在(不等于INF),则要先使dis[v][1]=dis[v][0],cnt[v][1]=cnt[v][0],然后更新dis[v][0]和cnt[v][0];
(2)dis[x][0]+w(x.v)==dis[v][0],则更新cnt[v][0];
(3)dis[v][0]<dis[x][0]+w(x,v)<dis[v][1],则更新dis[v][1]和cnt[v][1];
(4)dis[x][0]+w(x,v)==dis[v][1] ,则更新cnt[v][1]
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <stack>
#include <map>
using namespace std;
const int maxn=55;
const int INF=1000000000;
struct node
{
int u,dis,kind;
bool operator < (const node &a) const
{
if(dis!=a.dis)
return dis>a.dis;
return u>a.u;
}
};
struct node1
{
int v,w,next;
}edge[10000];
int head[maxn];
int dis[maxn][2],cnt[maxn][2];
bool vis[maxn][2];
int n,m,s,e,num;
void init()
{
for(int i=0;i<n;i++)
head[i]=-1;
num=0;
}
void add(int u,int v,int w)
{
edge[num].v=v;
edge[num].w=w;
edge[num].next=head[u];
head[u]=num++;
}
void dij()
{
priority_queue<node>Q;
node cur,next;
for(int i=0;i<n;i++)
{
dis[i][0]=dis[i][1]=INF;
}
memset(cnt,0,sizeof(cnt));
memset(vis,false,sizeof(vis));
dis[s][0]=0;
cnt[s][0]=1;
cur.u=s;
cur.dis=0;
cur.kind=0;
Q.push(cur);
while(!Q.empty())
{
cur=Q.top();
Q.pop();
if(vis[cur.u][cur.kind]) continue;
vis[cur.u][cur.kind]=true;
for(int i=head[cur.u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
int w=cur.dis+edge[i].w;
if(!vis[v][0]&&dis[v][0]>w) //若比最短路还要小
{
if(dis[v][0]!=INF) //若到v的最短路出现过,则更新次短路
{
dis[v][1]=dis[v][0];
cnt[v][1]=cnt[v][0];
next.dis=dis[v][1];
next.u=v;
next.kind=1;
Q.push(next);
}
dis[v][0]=w; //更新最短路
cnt[v][0]=cnt[cur.u][cur.kind];
next.kind=0;
next.dis=w;
next.u=v;
Q.push(next);
}
else if(!vis[v][0]&&dis[v][0]==w) //更新最短路径条数
cnt[v][0]+=cnt[cur.u][cur.kind];
else if(!vis[v][1]&&dis[v][1]>w) //更新次短路
{
dis[v][1]=w;
cnt[v][1]=cnt[cur.u][cur.kind];
next.kind=1;
next.u=v;
next.dis=w;
Q.push(next);
}
else if(!vis[v][1]&&dis[v][1]==w) //更新次短路径条数
cnt[v][1]+=cnt[cur.u][cur.kind];
}
}
printf("%d %d\n",dis[e][1],cnt[e][1]);
}
int main()
{
int a,b,c;
while(scanf("%d%d%d%d",&n,&m,&s,&e)!=EOF)
{
init();
while(m--)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
}
dij();
}
return 0;
}