SGU 185 Two shortest 最短路+最大流

185. Two shortest
time limit per test: 0.25 sec.
memory limit per test: 4096 KB
input: standard input
output: standard output



Yesterday Vasya and Petya quarreled badly, and now they don't want to see each other on their way to school. The problem is that they live in one and the same house, leave the house at the same time and go at the same speed by the shortest road. Neither of them wants to change their principles, that is why they want to find two separate shortest routes, which won't make them go along one road, but still they can meet at any junction. They ask you to help them. They number all the junctions with numbers from 1 to N (home and school are also considered as junctions). So their house has the number 1 and the school has the number N, each road connects two junctions exactly, and there cannot be several roads between any two junctions.

Input
The first line contains two integer numbers N and M (2<=N<=400), where M is the number of roads Petya and Vasya noticed. Each of the following M lines contains 3 integers: X, Y and L (1<=X, Y<=N, 1<=L<=10000), where X and Y - numbers of junctions, connected by the road and L is the length of the road.

Output
Write to the first line numbers of the junctions in the way they passed them on the first route. Write to the second line numbers of the junctions in the way they passed them on the second route. If it is impossible to help guys, then output "No solution".

Sample test(s)

Input
6 8
1 2 1
3 2 1
3 4 1
1 3 2
4 2 2
4 5 1
5 6 1
4 6 2

Output
1 3 4 5 6
1 2 4 6

题目大意:从一幅有向图中找到从1到n的两条没有公共边的最短路,加输出路径。

题目分析:用SPFA求一次最短路(SPFA较Dij编程复杂度简单,但效率略低),对所有在最短路上的边(u,v)建边(u,v,1),最后建立超级源点,建边(s,1,2),跑一次最大流,如果满流则有解,DFS输出解即可,否则输出无解。
PS:内存卡的比较紧,多多注意。

#include <stdio.h>
#include <string.h>
#include <algorithm>
#define REP(I, X) for(int I = 0; I < X; ++I)
#define FF(I, A, B) for(int I = A; I <= B; ++I)
#define clear(A, B) memset(A, B, sizeof A)
#define copy(A, B) memcpy(A, B, sizeof A)
#define min(A, B) ((A) < (B) ? (A) : (B))
#define max(A, B) ((A) > (B) ? (A) : (B))
using namespace std;
typedef long long ll;
typedef long long LL;
const int oo = 0x3f3f3f3f;
const int maxE = 200000;
const int maxN = 405;
const int maxQ = 10000;
struct Edge{
    int v, c, n;
};
Edge edge[maxE];
int adj[maxN], cntE;
int Q[maxQ], head, tail, inq[maxN];
int d[maxN], num[maxN], cur[maxN], pre[maxN];
int s, t, nv;
int n, m, dis[maxN], G[maxN][maxN];
void addedge(int u, int v, int c){
    edge[cntE].v = v; edge[cntE].c = c; edge[cntE].n = adj[u]; adj[u] = cntE++;
    edge[cntE].v = u; edge[cntE].c = 0; edge[cntE].n = adj[v]; adj[v] = cntE++;
}
void REV_BFS(){
    clear(d, -1);
    clear(num, 0);
    head = tail = 0;
    d[t] = 0;
    num[0] = 1;
    Q[tail++] = t;
    while(head != tail){
        int u = Q[head++];
        head %= maxQ;
        for(int i = adj[u]; ~i; i = edge[i].n){
            int v = edge[i].v;
            if(~d[v]) continue;
            d[v] = d[u] + 1;
            num[d[v]]++;
            Q[tail++] = v;
            tail %= maxQ;
        }
    }
}
int ISAP(){
    copy(cur, adj);
    REV_BFS();
    int flow = 0, u = pre[s] = s, i;
    while(d[s] < nv){
        if(u == t){
            int f = oo, neck;
            for(i = s; i != t; i = edge[cur[i]].v){
                if(f > edge[cur[i]].c){
                    f = edge[cur[i]].c;
                    neck = i;
                }
            }
            for(i = s; i != t; i = edge[cur[i]].v){
                edge[cur[i]].c -= f;
                edge[cur[i] ^ 1].c += f;
            }
            flow += f;
            u = neck;
        }
        for(i = cur[u]; ~i; i = edge[i].n) if(edge[i].c && d[u] == d[edge[i].v] + 1) break;
        if(~i){
            cur[u] = i;
            pre[edge[i].v] = u;
            u = edge[i].v;
        }
        else{
            if(0 == (--num[d[u]])) break;
            int mind = nv;
            for(i = adj[u]; ~i; i = edge[i].n){
                if(edge[i].c && mind > d[edge[i].v]){
                    mind = d[edge[i].v];
                    cur[u] = i;
                }
            }
            d[u] = mind + 1;
            num[d[u]]++;
            u = pre[u];
        }
    }
    return flow;
}
void SPFA(){
    clear(dis, oo);
    clear(inq, 0);
    dis[1] = 0;
    head = tail = 0;
    Q[tail++] = 1;
    while(head != tail){
        int u = Q[head++];
        head %= maxQ;
        inq[u] = 0;
        FF(i, 1, n) if(dis[i] > dis[u] + G[u][i]){
            dis[i] = dis[u] + G[u][i];
            if(!inq[i]){
                Q[tail++] = i;
                tail %= maxQ;
                inq[i] = 1;
            }
        }
    }
}
void DFS(int u){
    if(u == n){
        printf("%d\n", n);
        return;
    }
    printf("%d ", u);
    for(int i = adj[u]; ~i; i = edge[i].n){
        if(!edge[i].c && (i % 2 == 0)){
            edge[i].c = 1;
            DFS(edge[i].v);
            break;
        }
    }
}
void work(){
    int u, v, w;
    clear(adj, -1);
    clear(G, oo);
    cntE = 0;
    while(m--){
        scanf("%d%d%d", &u, &v, &w);
        G[u][v] = G[v][u] = w;
    }
    SPFA();
    s = 0; t = n; nv = t + 1;
    addedge(s, 1, 2);
    FF(u, 1, n) FF(v, 1, n) if(dis[v] == dis[u] + G[u][v]) addedge(u, v, 1);
    if(dis[n] == oo || 2 != ISAP()){
        printf("No solution\n");
        return;
    }
    REP(i, 2) DFS(1);
}
int main(){
    while(~scanf("%d%d", &n, &m)) work();
    return 0;
}



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值