How Many Paths Are There
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1266 Accepted Submission(s): 437
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
Author
ZSTU
Source
题意:有n个点,m条有向边,构成有向无环图。问从S点到T点次短路是多少及路的条数。
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
const int N = 55;
const int INF = 1<<30;
struct EDG
{
int to,next,dis;
} edg[N*N];
struct node
{
int dis,id,mark;
friend bool operator<(node aa,node bb)
{
return aa.dis>bb.dis;
}
};
int eid,head[N];
int dis[N][2],dp[N][2],in[N];
void addEdg(int u,int v,int d)
{
edg[eid].to=v;
edg[eid].dis=d;
edg[eid].next=head[u];
head[u]=eid++;
}
void topesort(int s,int n)
{
queue<int>q;
for(int i=0; i<n; i++)
if(in[i]==0&&i!=s)
{
q.push(i);
}
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u]; i!=-1; i=edg[i].next)
{
int v=edg[i].to;
in[v]--;
if(in[v]==0&&v!=s)
q.push(v);
}
}
for(int i=0; i<n; i++)
for(int j=0; j<2; j++)
dis[i][j]=INF,dp[i][j]=0;
q.push(s);
dis[s][0]=0;
dp[s][0]=1;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int k=0; k<2; k++)
for(int i=head[u]; i!=-1; i=edg[i].next)
{
int v=edg[i].to;
if(dis[u][k]+edg[i].dis<dis[v][0])
{
if(dis[v][0]!=INF)
{
dis[v][1]=dis[v][0];
dp[v][1]=dp[v][0];
}
dis[v][0]=dis[u][k]+edg[i].dis;
dp[v][0]=dp[u][k];
}
else if(dis[u][k]+edg[i].dis==dis[v][0])
dp[v][0]+=dp[u][k];
else if(dis[u][k]+edg[i].dis<dis[v][1])
{
dis[v][1]=dis[u][k]+edg[i].dis;
dp[v][1]=dp[u][k];
}
else if(dis[u][k]+edg[i].dis==dis[v][1])
dp[v][1]+=dp[u][k];
if(in[v])
{
in[v]--;
if(in[v]==0)
q.push(v);
}
}
}
}
int main()
{
int n,m,s,t,u,v,d;
while(scanf("%d%d%d%d",&n,&m,&s,&t)>0)
{
eid=0;
memset(head,-1,sizeof(head));
memset(in , 0, sizeof(in));
while(m--)
{
scanf("%d%d%d",&u,&v,&d);
addEdg(u,v,d);
in[v]++;
}
topesort(s , n);
printf("%d %d\n",dis[t][1],dp[t][1]);
}
}