您的位置:首页 > 其它

HDU 3191 How Many Paths Are There

2014-09-29 23:16 387 查看

How Many Paths Are There

Time Limit: 1000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 3191
64-bit integer IO format: %I64d Java class name: Main

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

Source

HDU 2009-12 Programming Contest

解题:Dijkstra求次短路

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 55;
struct arc {
int to,w,next;
arc(int x = 0,int y = 0,int z = -1) {
to = x;
w = y;
next = z;
}
};
arc e[10000];
int head[maxn],d[maxn][2],cnt[maxn][2],tot,big,ed,n,m;
bool vis[maxn][2];
void add(int u,int v,int w){
e[tot] = arc(v,w,head[u]);
head[u] = tot++;
}
void dijkstra(){
for(int i = 0; i <= n; i++){
vis[i][0] = vis[i][1] = false;
cnt[i][0] = cnt[i][1] = 0;
d[i][0] = d[i][1] = INF;
}
d[big][0] = 0;
cnt[big][0] = 1;
while(true){
int u,Min = INF,flag;
for(int i = 0; i < n; i++){
if(!vis[i][0] && d[i][0] < Min)
Min = d[u = i][flag = 0];
else if(!vis[i][1] && d[i][1] < Min)
Min = d[u = i][flag = 1];
}
if(u == ed && flag || Min == INF) break;
vis[u][flag] = true;
for(int i = head[u]; ~i; i = e[i].next){
int v = e[i].to;
int w = d[u][flag] + e[i].w;
if(d[v][0] > w){
if(d[v][0] < INF){
d[v][1] = d[v][0];
cnt[v][1] = cnt[v][0];
}
d[v][0] = w;
cnt[v][0] = cnt[u][flag];
}else if(d[v][0] == w)
cnt[v][0] += cnt[u][flag];
else if(d[v][1] > w){
d[v][1] = w;
cnt[v][1] = cnt[u][flag];
}else if(d[v][1] == w) cnt[v][1] += cnt[u][flag];
}
}
printf("%d %d\n",d[ed][1],cnt[ed][1]);
}
int main(){
int u,v,w;
while(~scanf("%d %d %d %d",&n,&m,&big,&ed)){
memset(head,-1,sizeof(head));
for(int i = tot = 0; i < m; i++){
scanf("%d %d %d",&u,&v,&w);
add(u,v,w);
}
dijkstra();
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: