I - Travel
Time Limit:0MS Memory Limit:0KB 64bit IO Format:%lld & %llu
Submit Status
Description
Travel
The country frog lives in has n
towns which are conveniently numbered by 1,2,…,n
.
Among n(n?1)2
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 b
minutes to travel.
Find the minimum(最小的) time to travel from town 1
to town n
.
Input
The input(投入) consists of multiple tests. For each test:
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
).
Output
For each test, write 1
integer(整数) which denotes(表示) the minimum(最小的) time.
Sample Input
3 2 1 3
1 2
2 3
3 2 2 3
1 2
2 3
Sample Output
2
②1和n通过铁路相连,直接到达n的时间为b,那么就要考虑是直接用时b到达n,还是经过若干个a到达n
大神 博客 http://blog.youkuaiyun.com/lvshubao1314/article/details/48848319
Time Limit:0MS Memory Limit:0KB 64bit IO Format:%lld & %llu
Submit Status
Description
Travel
The country frog lives in has n
towns which are conveniently numbered by 1,2,…,n
.
Among n(n?1)2
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 b
minutes to travel.
Find the minimum(最小的) time to travel from town 1
to town n
.
Input
The input(投入) consists of multiple tests. For each test:
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
).
Output
For each test, write 1
integer(整数) which denotes(表示) the minimum(最小的) time.
Sample Input
3 2 1 3
1 2
2 3
3 2 2 3
1 2
2 3
Sample Output
2
3
题意:有n个城市,编号为1~n,每个城市都相互连通,其中有m对城市通过公路连通,其他的城市通过铁路连通,经过公路的时间为a,经过铁路的时间为b,问从1到达n的时间最短为多少.
题解:求最短路问题,但是这道题中,每个点都是相连通的,不过连通的边有2种,那么从1走到n就可以分成2种情况:
①1和n通过公路相连,直接到达n的时间为a,那么就要考虑是直接用时a到达n,还是经过若干个b到达n②1和n通过铁路相连,直接到达n的时间为b,那么就要考虑是直接用时b到达n,还是经过若干个a到达n
大神 博客 http://blog.youkuaiyun.com/lvshubao1314/article/details/48848319
因此我们就能针对1与n的连通情况进行bfs,如果间接到达n的时间cnt要小于直接通过相连的路到达的时间t,那么最短时间就是cnt,反之,答案则为t对于情况
②我们很容易想到用SPFA做,不过情况①的边有n(n-1)/2-m条,显然用SPFA会超时.我们可以发现,要间接到达n,通过的点数绝对不会超过n-1,因为要走的路不能构成环,那么我们可以用set储存2~n,当目前点的编号为u,搜索与u通过a或者b相连的点v,存入队列,然后将v从set中删除,最后就能得到间接到达n点的最短路
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <set>//相同的数 不共存
#include <queue>
#define inf 0x3f3f3f3f
using namespace std;
long long dist[100010];
int head[100010];
bool vis[100010];
set<int>st,ts; //定义 两个集合
set<int>::iterator it; //迭代器
struct Node
{
int v;
int w;
int next;
}edge[1000010];
long long a,b;
int n,m,num;
void add_edge(int u,int v)
{
edge[num].v = v;
edge[num].w = a;
edge[num].next = head[u];
head[u] = num++;
}
long long spfa()
{
int u,v,i,w;
queue<int> q;
q.push(1);
memset(dist,inf,sizeof(dist));
memset(vis,false,sizeof(vis));
dist[1] = 0;
vis[1] = true;
while(!q.empty())
{
u = q.front();
q.pop();
vis[u] = false;
for(i=head[u];i!=-1;i=edge[i].next)
{
v = edge[i].v;
w = edge[i].w;
if(dist[v]>dist[u]+w)
{
dist[v] = dist[u] + w;
if(!vis[v])
{
vis[v] = true;
q.push(v);
}
}
}
}
return dist[n]<b ? dist[n]:b;
}
long long bfs()
{
int i,u,v;
dist[n]=inf;
st.clear(); ts.clear();
for(i=2;i<=n;i++)
{
st.insert(i);//把数放入集合中
}
queue<int>q;
q.push(1);
dist[1]=0;
while(!q.empty())
{
u=q.front();
q.pop();
for(i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].v;
if(st.count(v)==0)//判断当前的数 是否在集合中
{
continue;
}
st.erase(v);//从当前集合中移除
ts.insert(v);
}
for(it=st.begin();it!=st.end();it++)
{
q.push(*it);
dist[*it]=dist[u]+1;
}
st.swap(ts); //把两集合内的元素互换
ts.clear(); //当前集合内的元素删去
}
return dist[n]*b<a ? dist[n]*b:a;
}
int main()
{
while(scanf("%d%d%lld%lld",&n,&m,&a,&b)!=EOF)
{
int i,j,u,v;
bool f = false;
memset(head,-1,sizeof(head));
for(i=0;i<m;i++)
{
scanf("%d%d",&u,&v);
if(u>v)
{
swap(u,v);
}
add_edge(u,v);
add_edge(v,u);
if(u==1 && v==n)
{
f = true;
}
}
num = 0;
if(f)
{
printf("%lld\n",bfs());
}
else
{
printf("%lld\n",spfa());
}
}
return 0;
}