Description
After last year's edition of the BAPC, you were still stuck in Delft. In order to participate again this year, you is going to Amsterdam by bus. During The journey you look out of the the window and look for traffic signs, that point in the direction of Amsterdam. To your surprise, you notice the bus was never taking the roads that was pointed out by the signs! You think this bus company might has chosen a route such that, at no intersection, the bus goes in the direction that Is pointed to by the signs. Your friends, however, find this very unbelievable, and don ' t think the is possible. Can you figure out whether there exists a bus route which satisfies this requirement? Note that a bus route never visits the same place twice. A traffic sign pointing in the direction of the shortest route to Amsterdam are placed at every intersection. Could assume that the input graph was both simple and connected, and that there was a unique optimal route to Amsterdam fr Om every intersection.Input
a single line containing-Integers:n (2≤n≤105), the number of intersections, and M (1≤m≤106), the number O F undirected roads that connect the intersections. The intersections is numbered from 0 to n−1. Delft is denoted by intersection i = 0 and Amsterdam are denoted by intersection i = 1.
• m lines that specify the Roads–a road are specified by three integers, AI, bi (0≤ai, bi < n and ai 6= bi) and di (0≤di≤500000), where AI and bi are IDs of the-the-intersections that's connected by this road and Di is the Distanc E that the bus had to travel to get from AI to bi or vice versa. Output
As output, give one of the following:
a path from Delft to Amsterdam this satisfies the requirements, in case such A path exists. –a path is specified by A single line containing an integer k, the length of the path, followed by k integers pi that spe Cify the intersections along the path in the order in which they is crossed, with P0 = 0 and pk−1 = 1.
the text "impossible", if such a path does not exist. Sample
Sample Input 1
4 5
0 2 5
2 1 5 0
3
3 1 3 2 5
Sample Output 1
3 0 3 1
sample INP UT 2
4 3
0 1 1 2
2 3
sample Output 2
Impossible
sample Input 3
7
9 0 1-
6 2 3 2 3-4 4-5 4-1-1-6-0-2-0-3-
Sample out Put 3
2 0 1
Title Description
Existing n Junction, each intersection of the road signs will be marked to 1 junction of the shortest point of the next node, now judge from the 0->1 and can not walk the direction of the mark, whether there is an answer. If there is an output path, the output is "impossible". Thinking of solving problems
The
Tag source point is 1, the node indicated on the road sign of each intersection is marked with Dijkstar, and then the marked route is not feasible, and the direct DFS burst searches for the marked feasible path. Code Implementation
#include <bits/stdc++.h> #define IO Ios::sync_with_stdio (false); \ cin.tie (0); \ cout.tie (0);
using namespace Std;
typedef long Long LL;
const int mod = 1E9+7;
const int inf=0x3f3f3f3f;
const int MAXN = 1E6+7;
int tot;
int DIST[MAXN],HEAD[MAXN],PRE[MAXN];
BOOL VIS[MAXN], flag;
int ans[maxn],len=0;
struct node {int u;
int V;
int C;
int NE;
} EDGE[MAXN];
struct Qnode {int v;
int C;
Qnode (int _v=0,int _c=0): V (_v), C (_c) {} BOOL operator < (const qnode &r) Const {return c>r.c;
}
};
void Init () {memset (pre,-1,sizeof (pre));
memset (vis,0,sizeof (VIS));
memset (dist,inf,sizeof (Dist));
memset (head,-1,sizeof (head));
tot=0;
Flag=false;
} void Addedge (int u,int v,int cost) {edge[tot].u=u;
Edge[tot].v=v;
Edge[tot].c=cost;
Edge[tot].ne=head[u];
head[u]=tot++;
} void Dijkstra (int n,int start)//point number starting with 1 {priority_queue<qnode>que;
while (!que.empty ()) Que.pop (); Dist[start]=0;
Que.push (Qnode (start,0));
Qnode tmp;
while (!que.empty ()) {tmp=que.top ();
Que.pop ();
int u=tmp.v;
if (Vis[u]) continue;
Vis[u]=true;
for (int i=head[u];i!=-1;i=edge[i].ne) {int v=edge[i].v;
int cost=edge[i].c;
if (!vis[v] && dist[v]>dist[u]+cost) {Pre[v] = u;
DIST[V] = Dist[u]+cost;
Que.push (Qnode (v,dist[v));
}}}} void Print_ans (int point) {if (point==1) {cout<< "" <<0;
return;
} Print_ans (point-1);
cout<< "" <<ans[point]-1;}
void Dfs (int tmp,int pree) {ans[++len]=tmp;
Vis[tmp]=1;
if (tmp==2) {flag=true;
cout<<len;
Print_ans (len);
cout<<endl;
return;
} for (int i=head[tmp];i!=-1;i=edge[i].ne) {int v=edge[i].v; if (pre[Tmp]==v | |
VIS[V]) continue;
DFS (V,TMP);
if (flag) return;
}--len;
} int main () {IO;
Init ();
int n,m;
int u,v,c;
cin>>n>>m;
for (int i=0;i<m;i++) {cin>>u>>v>>c;
Addedge (U+1,V+1,C);
Addedge (V+1,U+1,C);
} Dijkstra (n,2);
memset (vis,0,sizeof (VIS));
DFS (1,-1);
if (!flag) cout<< "Impossible" <<endl;
return 0;
}
Ps:dijkstar optimization algorithm template (from Kuangbin)
/* * Use Priority queue optimization Dijkstra algorithm * complexity O (eloge) * Note to VECTOR<EDGE>E[MAXN] after initialization */const int inf=0x3f3f3f3f;
const int maxn=1000010;
struct Qnode {int v;
int C;
Qnode (int _v=0,int _c=0): V (_v), C (_c) {} BOOL operator < (const qnode &r) Const {return c>r.c;
}
};
struct Edge {int v,cost;
Edge (int _v=0,int _cost=0): V (_v), cost (_cost) {}};
vector<edge>e[maxn];
BOOL VIS[MAXN];
int DIST[MAXN];
void Dijkstra (int n,int start)//point number starting from 1 {memset (vis,false,sizeof (VIS));
for (int i=1; i<=n; i++) Dist[i]=inf;
priority_queue<qnode>que;
while (!que.empty ()) Que.pop ();
dist[start]=0;
Que.push (Qnode (start,0));
Qnode tmp;
while (!que.empty ()) {tmp=que.top ();
Que.pop ();
int u=tmp.v;
if (Vis[u]) continue;
Vis[u]=true;
for (int i=0; i<e[u].size (); i++) {int v=e[tmp.v][i].v;
int cost=e[u][i].cost; if (!vis[v]&&dist[v]>dist[u]+cost) {dist[v]=dist[u]+cost;
Que.push (Qnode (v,dist[v));
}}}} void Addedge (int u,int v,int W) {E[u].push_back (Edge (V,w));}