How Many Paths Are There

来源:互联网 发布:淘宝店运营视频教程 编辑:程序博客网 时间:2024/05/01 06:58

How Many Paths Are There

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 159    Accepted Submission(s): 45

 
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 20 2 50 1 41 2 2
Sample Output
6 1
 
u, v, 0这种边,应该保证u先于v更新,题目测试数据可能在这种情况下,u < v 因此对顶点序号排序后的优先队列可以ac。
 
#include<cstdio>#include<queue>#include<cstring>#define INF 1e8using namespace std;const int MAXN = 50 + 5;const int MAXM = 2500;int u[MAXM], v[MAXM], w[MAXM], next[MAXM];int dis[MAXN][2], cnt[MAXN][2], done[MAXN][2], first[MAXN];//dis[i][0,1]表示到i的最短距离及次短距离int n, m, s, e; struct Node {int d;int flag;int from;bool operator < (const Node &a) const {if(a.d != d)return d > a.d;else return from > a.from;}};void init(){for(int i = 0; i < n; i++){dis[i][0] = dis[i][1] = INF;done[i][0] = done[i][1] = 0;cnt[i][0] = cnt[i][1] = 0;}}void dijkstra(){Node node;priority_queue<Node> q;init();dis[s][0] = 0;cnt[s][0] = 1;node.d = dis[s][0];node.flag = 0;node.from = s;q.push(node);while(!q.empty()){node = q.top();q.pop();Node pres;if(done[node.from][node.flag]) continue;done[node.from][node.flag] = 1;for(int e = first[node.from]; e != -1; e = next[e]){int to = v[e];if(!done[to][0] && dis[to][0] > node.d + w[e]){//更新最短路径if(dis[to][0] < INF){//更新次短路径 dis[to][1] = dis[to][0];cnt[to][1] = cnt[to][0];pres.d = dis[to][1];pres.flag = 1;pres.from = to;q.push(pres);} dis[to][0] = node.d + w[e];cnt[to][0] = cnt[node.from][node.flag];pres.d = dis[to][0];pres.flag = 0;pres.from = to;q.push(pres);}else if(!done[to][0] && dis[to][0] == node.d + w[e]){cnt[to][0] += cnt[node.from][node.flag];}else if(!done[to][1] && dis[to][1] > node.d + w[e]){dis[to][1] = node.d + w[e];cnt[to][1] = cnt[node.from][node.flag];pres.d = dis[to][1];pres.flag = 1;pres.from = to;q.push(pres);}else if(!done[to][1] && dis[to][1] == node.d + w[e]){cnt[to][1] += cnt[node.from][node.flag];}}}/*6 8 0 10 1 10 2 12 1 01 3 01 4 13 4 02 5 05 1 0*/}int main(){while(~scanf("%d%d%d%d", &n, &m, &s, &e)){memset(first, -1, sizeof(first));for(int i = 0; i < m; i++){scanf("%d%d%d", &u[i], &v[i], &w[i]);next[i] = first[u[i]];first[u[i]] = i;}dijkstra();printf("%d %d\n", dis[e][0], cnt[e][0]);}return 1;}

0 0
原创粉丝点击