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 2Sample Output
6 1Author
ZSTU分析:
次短路条数板子 简单总结一下: 次短路条数选择dij, 每个节点记录两个状态:最短路和次短路,同时分别记录条数 dij进行2*n-1次 每次找出所有可用路径中的最短路径 在这个基础上进行更改 设当前的最短距离是x=dis[k][flag]+way[i].v 一共有四种情况: x < dis[j][0] x==dis[j][0] x < dis[j][1] x==dis[j][1] 分别转移就好了tip
因为结点的编号从0开始,我的处理方法是把ta变成从1开始
不要忘了if (mn==INF) break;
不存在最短的转移点,就退出for循环, 不要忘了bool数组的置否 注意这句话
You would be given a directed graph G
译:给出一个有向图
又是有向边
可见,英文题面中许多条件不会再hint中中专门给出,一定要
学好英语,仔细读题
听学长说北大冬令营题面几乎都是英文的,所以好好学英文吧
这里写代码片#include#include #include using namespace std;const int INF=0x3333333;const int N=100;struct node{ int x,y,v,nxt;};node way[N*N];int cnt[N][2],st[N],tot,dis[N][2],n,m,s,t;bool p[N][2];void add(int u,int w,int z){ tot++; way[tot].x=u;way[tot].y=w;way[tot].v=z;way[tot].nxt=st[u];st[u]=tot;}void dij(int s,int t){ int i,j,k,flag; memset(dis,0x33,sizeof(dis)); memset(p,1,sizeof(p)); memset(cnt,0,sizeof(cnt)); dis[s][0]=0; cnt[s][0]=1; for (i=1;i<=2*n-1;i++) { int mn=INF; for (j=1;j<=n;j++) if (p[j][0]&&dis[j][0] mn+w) { dis[y][1]=mn+w; cnt[y][1]=cnt[k][flag]; } else if (dis[y][1]==mn+w) { cnt[y][1]+=cnt[k][flag]; } } } printf("%d %d\n",dis[t][1],cnt[t][1]);}int main(){ while (scanf("%d%d%d%d",&n,&m,&s,&t)!=EOF) { s++; t++; tot=0; memset(st,0,sizeof(st)); for (int i=1;i<=m;i++) { int u,w,z; scanf("%d%d%d",&u,&w,&z); u++; w++; add(u,w,z); //add(w,u,z); } dij(s,t); } return 0;}