【
题意】给出一个图,求 1 -> n的2条 没有重边的最短路。
真◆神题……卡内存卡得我一脸血= =……
【
思路】
一开始我的想法是
两遍Dijkstra做一次删一次边不就行了么你们还又Dijkstra预处理又最大流的Too naive……结果事实证明从来都是我naive= =……明显是不行的……最大流可能有好几条……但不重边的更少……也许第一次Dijkstra找到的是最短路但不是最后不重边的最短路,然后就这么把边删了显然不对……
所以我们还是言归正解吧……这道题就是
ZOJ 2760的升级版吧……也是,可以floyd可以源汇点Dij预处理先筛出最短路上的边加入到网络流中,容量限制为2。那么最大流==2就有解,然后沿着满流边dfs 2遍就找到这两条路了~
次奥卡内存卡的啊……只能用4000KB内存!!最后还是改了两个short int才勉强过了T_T……
#include
#include
#include
#include
#include
#include
#include
#include
#define MID(x,y) ((x+y)/2)
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int MAXV = 405;
const int MAXE = 170055;
const int oo = 0x3fffffff;
short int map[MAXV][MAXV];
int ds[MAXV], dt[MAXV];
void dijkstra(int *dist, int s, int t, int n){
bool vis[MAXV] = {0};
for (int i = 0; i <= n; i ++){
dist[i] = oo;
}
dist[s] = 0;
for(int p = 1; p <= n; p ++){
int minx = oo;
int u;
for (int i = 1; i <= n; i ++){
if (!vis[i] && dist[i] < minx){
minx = dist[i];
u = i;
}
}
if (minx == oo)
break;
vis[u] = 1;
for (int v = 1; v <= n; v ++){
if (v == u || vis[v] || map[u][v] == 30000) continue;
if (dist[v] > dist[u] + map[u][v]){
dist[v] = dist[u] + map[u][v];
}
}
}
}
struct node{
short int u, v;
int flow;
int next;
};
struct Dinic{
node arc[MAXE];
int vn, en, head[MAXV]; //vn点个数(包括源点汇点),en边个数
int cur[MAXV]; //当前弧
int q[MAXV]; //bfs建层次图时的队列
int path[MAXE], top; //存dfs当前最短路径的栈
int dep[MAXV]; //各节点层次
void init(int n){
vn = n;
en = 0;
mem(head, -1);
}
void insert_flow(int u, int v, int flow){
arc[en].u = u;
arc[en].v = v;
arc[en].flow = flow;
arc[en].next = head[u];
head[u] = en ++;
arc[en].u = v;
arc[en].v = u;
arc[en].flow = 0; //反向弧
arc[en].next = head[v];
head[v] = en ++;
}
bool bfs(int s, int t){
mem(dep, -1);
int lq = 0, rq = 1;
dep[s] = 0;
q[lq] = s;
while(lq < rq){
int u = q[lq ++];
if (u == t){
return true;
}
for (int i = head[u]; i != -1; i = arc[i].next){
int v = arc[i].v;
if (dep[v] == -1 && arc[i].flow > 0){
dep[v] = dep[u] + 1;
q[rq ++] = v;
}
}
}
return false;
}
int solve(int s, int t){
int maxflow = 0;
while(bfs(s, t)){
int i, j;
for (i = 1; i <= vn; i ++) cur[i] = head[i];
for (i = s, top = 0;;){
if (i == t){
int mink;
int minflow = 0x3fffffff;
for (int k = 0; k < top; k ++)
if (minflow > arc[path[k]].flow){
minflow = arc[path[k]].flow;
mink = k;
}
for (int k = 0; k < top; k ++)
arc[path[k]].flow -= minflow, arc[path[k]^1].flow += minflow;
maxflow += minflow;
top = mink; //arc[mink]这条边流量变为0, 则直接回溯到该边的起点即可(这条边将不再包含在增广路内).
i = arc[path[top]].u;
}
for (j = cur[i]; j != -1; cur[i] = j = arc[j].next){
int v = arc[j].v;
if (arc[j].flow && dep[v] == dep[i] + 1)
break;
}
if (j != -1){
path[top ++] = j;
i = arc[j].v;
}
else{
if (top == 0) break;
dep[i] = -1;
i = arc[path[-- top]].u;
}
}
}
return maxflow;
}
}dinic;
int pre[MAXV];
bool vis[MAXV];
stack path;
int ok;
void dfs(int u, int n){
if (u == n){
ok = 1;
while(!path.empty()){
path.pop();
}
while(pre[u] != -1){
path.push(u);
dinic.arc[pre[u]].flow = 1;
dinic.arc[pre[u]^1].flow = 1;
u = dinic.arc[pre[u]].u;
}
return ;
}
vis[u] = 1;
for (int i = dinic.head[u]; i != -1; i = dinic.arc[i].next){
if (i % 2 == 1 || dinic.arc[i].flow != 0) continue;
int v = dinic.arc[i].v;
if (vis[v]) continue;
pre[v] = i;
dfs(v, n);
if (ok)
return ;
pre[v] = -1;
}
return ;
}
int main(){
//freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
int n, m;
scanf("%d %d", &n, &m);
for (int i = 0; i <= n; i ++){
for (int j = 0; j <= n; j ++){
map[i][j] = 30000;
}
}
for (int i = 0; i < m; i ++){
int u, v, w;
scanf("%d %d %d", &u, &v, &w);
map[u][v] = map[v][u] = w;
if (u == v)
map[u][v] = 0;
}
dijkstra(ds, 1, n, n);
dijkstra(dt, n, 1, n);
dinic.init(n+2);
dinic.insert_flow(n+1, 1, 2);
dinic.insert_flow(n, n+2, 2);
for (int i = 1; i <= n; i ++){
for (int j = 1; j <= n; j ++){
if (i == j) continue;
if (ds[i] + map[i][j] + dt[j] == ds[n]){
dinic.insert_flow(i, j, 1);
}
}
}
if (dinic.solve(n+1, n+2) == 2){
mem(pre, -1);
mem(vis, 0);
ok = 0;
dfs(1, n);
printf("1");
while(!path.empty()){
printf(" %d", path.top());
path.pop();
}
puts("");
mem(pre, -1);
mem(vis, 0);
ok = 0;
dfs(1, n);
printf("1");
while(!path.empty()){
printf(" %d", path.top());
path.pop();
}
puts("");
}
else{
puts("No solution");
}
return 0;
}