The country frog lives in has nnode
towns which are conveniently numbered by 1,2,…,nios
.spa
Among n(n−1)2code
pairs of towns, m of them are connected by bidirectional highway, which needs a minutes to travel. The other pairs are connected by railway, which needs bxml
minutes to travel.blog
Find the minimum time to travel from town 1ip
to town nci
.input
The input consists of multiple tests. For each test:string
The first line contains 4
integers n,m,a,b (2≤n≤105,0≤m≤5⋅105,1≤a,b≤109). Each of the following m lines contains 2 integers ui,vi, which denotes cities ui and vi are connected by highway. (1≤ui,vi≤n,ui≠vi
).
For each test, write 1
integer which denotes the minimum time.
3 2 1 3 1 2 2 3 3 2 2 3 1 2 2 3
分兩種狀況:
1. 1到 n 鐵路能夠直連 跑一次最短路取 Min(b,dist[n])
2. 1 到n 高速公路連通 那麼咱們BFS跑最短路 ,每次構一次新圖,每一個點入隊一次.由於是徹底圖因此每次能夠入隊的點都很是多.因此能夠暴力.
2 3
#include <iostream> #include <cstdio> #include <string.h> #include <queue> #include <algorithm> #include <math.h> using namespace std; typedef long long LL; const LL INF = 1e16; const LL N = 100005; struct Edge{ LL v,next; LL w; }edge[10*N]; LL head[N]; LL tot,n,m,a,b; bool vis[N]; LL low[N]; LL MIN ; void addEdge(LL u,LL v,LL w,LL &k){ edge[k].v = v,edge[k].w = w,edge[k].next = head[u],head[u]=k++; } struct Node{ int u; int step; }; void init(){ memset(head,-1,sizeof(head)); tot = 0; } void spfa(LL pos){ for(LL i=1;i<=n;i++){ low[i] = INF; vis[i] = false; } low[pos] = 0; queue<LL>q; while(!q.empty()) q.pop(); q.push(pos); while(!q.empty()){ LL u = q.front(); q.pop(); vis[u] = false; for(LL k=head[u];k!=-1;k=edge[k].next){ LL w = edge[k].w,v = edge[k].v; if(low[v]>low[u]+w){ low[v] = low[u]+w; if(!vis[v]){ vis[v] = true; q.push(v); } } } } } bool vis1[N]; int bfs(int pos){ memset(vis,0,sizeof(vis)); queue<Node> q; Node node; vis[pos] = 1; node.u = pos; node.step = 0; q.push(node); while(!q.empty()){ memset(vis1,false,sizeof(vis1)); node = q.front(); int u = node.u; int step = node.step; /// if((step+1)*b>a) return -1; TLE q.pop(); vis1[u] = 1; for(int k=head[u];k!=-1;k=edge[k].next){ int v = edge[k].v; vis1[v] = true; } Node next; if(!vis1[n]) return step+1; for(int i=n;i>=1;i--){ if(!vis1[i]&&!vis[i]){ if((step+1)*b>a) return -1; next.u = i; next.step = step+1; q.push(next); vis[i] = true; } } } return -1; } int main(){ while(scanf("%lld%lld%lld%lld",&n,&m,&a,&b)!=EOF){ init(); bool flag = false; for(int i=0;i<m;i++){ LL u,v; scanf("%lld%lld",&u,&v); addEdge(u,v,a,tot); addEdge(v,u,a,tot); if(u==1&&v==n||u==n&&v==1) flag = 1; } LL ans = 0; if(!flag){ spfa(1); ans = min(low[n],b); }else{ int step = bfs(1); if(step==-1) ans = a; else ans = min(a,step*b); } printf("%lld\n",ans); } }