CodeForces 20C Dijkstra?
2018-01-23 19:34
519 查看
题目
ou are given a weighted undirected graph. The vertices are enumerated from 1 to
n. Your task is to find the shortest path between the vertex
1 and the vertex n.
Input
The first line contains two integers n and
m (2 ≤ n ≤ 105, 0 ≤ m ≤ 105), where
n is the number of vertices and
m is the number of edges. Following m lines contain one edge each in form
ai,
bi and
wi (1 ≤ ai, bi ≤ n, 1 ≤ wi ≤ 106),
where ai, bi are edge endpoints and
wi is the length of the edge.
It is possible that the graph has loops and multiple edges between pair of vertices.
Output
Write the only integer -1 in case of no path. Write the shortest path in opposite case. If there are many solutions, print any of them.
Examples
Input
Output
Input
Output
其实上spfa也可以很快解决。很基础的东西, 直接套模板,改一些小地方就可以。但是我之前居然天真的以为一定要链式向前星,hhh。基础的spfa加这样一句话就可以
if(dis[now]>dis
) continue;
就是34的位置。就单单这一句话让我的时间从超时(1000ms+)->93ms 它判断掉了大量的无用情况,对于这道题目来说节省了很多时间,坏处是本来做一次spfa,那么每个dis[i]都是从st到i的最短距离,但现在这个是做不到的,只能保证是dis
的最短距离。
代码1
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<math.h>
#include<algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
const int maxm=1e5+5;
const ll inf=0x3f3f3f3f3f3f3f3f;
ll dis[maxn];
bool vis[maxn];
int path[maxn];//存前一个节点//
struct node{
int to,cost;
};
vector<node> e[maxn];
void spfa(int st,int n){
mem(dis,inf);
mem(vis,false);
queue<int> que;
dis[st]=0;
que.push(st);
while(!que.empty()){
int now=que.front();
que.pop();
vis[now]=false;
if(dis[now]>dis
) continue;
int size=e[now].size();
for(int i=0;i<size;i++){
int to=e[now][i].to;
int cost=e[now][i].cost;
if(dis[to]>dis[now]+cost){
dis[to]=dis[now]+cost;
path[to]=now;
if(!vis[to]){
vis[to]=true;
que.push(to);
}
}
}
}
}
int main(){
std::ios::sync_with_stdio(false);
int n,m;
scanf("%d %d",&n,&m);
for(int i=0;i<m;i++){
int x,y,cost;
scanf("%d %d %d",&x,&y,&cost);
e[x].push_back((node){y,cost});
e[y].push_back((node){x,cost});
}
spfa(1,n);
int a[maxn];
// cout<<dis
<<endl;
// cout<<inf<<endl;
mem(a,-1);
if(dis
==inf) cout<<-1<<endl;
else{
int k=0;
a[k]=n;
k++;
for(int i=n;i>1;){
int j=path[i];
a[k]=j;
i=j;
k++;
}
for(int i=k-1;i>=0;i--){
cout<<a[i];
if(i>=1) cout<<" ";
else cout<<endl;
}
}
}然后是链式向前星,中间的自主学习推荐一个链接。
理解后同样运用spfa,时间同样被削减到了78ms.要提的是中间re过,原因是用链式向前星的时候。e的数组大小要为m的2倍,因为对于无向图每条边要存2次!
代码2
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<math.h>
#include<algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
const int maxm=1e6+5;
const ll inf=0x3f3f3f3f3f3f3f3f;
ll dis[maxn];
bool vis[maxn];
int path[maxn];//存前一个节点//
ll head[maxn];
struct node{
ll to,cost,next;
}e[2*maxn];
ll cnt=0;
void spfa(ll st){
mem(dis,inf);
mem(vis,false);
queue<ll> que;
dis[st]=0;
que.push(st);
while(!que.empty()){
int now=que.front();
que.pop();
vis[now]=false;
/* int size=e[now].size();
for(int i=0;i<size;i++){
int to=e[now][i].to;
int cost=e[now][i].cost;
if(dis[to]>dis[now]+cost){
dis[to]=dis[now]+cost;
path[to]=now;
if(!vis[to]){
vis[to]=true;
que.push(to);
}
}
}*/
for(ll i=head[now];i!=-1;i=e[i].next){
ll to=e[i].to;
ll cost=e[i].cost;
if(dis[to]>dis[now]+cost){
dis[to]=dis[now]+cost;
path[to]=now;
if(!vis[to]){
vis[to]=true;
que.push(to);
}
}
}
}
}
void add(ll a, ll b , ll c) {
e[cnt].cost=c;
e[cnt].to=b;
e[cnt].next=head[a];
head[a] = cnt++;
}
int main(){
// std::ios::sync_with_stdio(false);
ll n,m;
scanf("%I64d %I64d",&n,&m);
mem(head,-1);
for(ll i=0;i<m;i++){
ll x,y,cost;
scanf("%I64d %I64d %I64d",&x,&y,&cost);
add(x,y,cost);
add(y,x,cost);
}
/* for(int i=0;i<=cnt;i++){
cout<<e[i].to<<" "<<e[i].cost<<" "<<e[i].next<<endl;
}*/
spfa(1);
ll a[maxn];
// cout<<dis
<<endl;
// cout<<inf<<endl;
mem(a,-1);
if(dis
==inf) cout<<-1<<endl;
else{
ll k=0;
a[k]=n;
k++;
for(ll i=n;i>1;){
ll j=path[i];
a[k]=j;
i=j;
k++;
}
for(ll i=k-1;i>=0;i--){
printf("%I64d",a[i]);
if(i>=1)printf(" ");
else printf("\n");
}
}
}
ou are given a weighted undirected graph. The vertices are enumerated from 1 to
n. Your task is to find the shortest path between the vertex
1 and the vertex n.
Input
The first line contains two integers n and
m (2 ≤ n ≤ 105, 0 ≤ m ≤ 105), where
n is the number of vertices and
m is the number of edges. Following m lines contain one edge each in form
ai,
bi and
wi (1 ≤ ai, bi ≤ n, 1 ≤ wi ≤ 106),
where ai, bi are edge endpoints and
wi is the length of the edge.
It is possible that the graph has loops and multiple edges between pair of vertices.
Output
Write the only integer -1 in case of no path. Write the shortest path in opposite case. If there are many solutions, print any of them.
Examples
Input
5 6 1 2 2 2 5 5 2 3 4 1 4 1 4 3 3 3 5 1
Output
1 4 3 5
Input
5 6 1 2 2 2 5 5 2 3 4 1 4 1 4 3 3 3 5 1
Output
1 4 3 5
其实上spfa也可以很快解决。很基础的东西, 直接套模板,改一些小地方就可以。但是我之前居然天真的以为一定要链式向前星,hhh。基础的spfa加这样一句话就可以
if(dis[now]>dis
) continue;
就是34的位置。就单单这一句话让我的时间从超时(1000ms+)->93ms 它判断掉了大量的无用情况,对于这道题目来说节省了很多时间,坏处是本来做一次spfa,那么每个dis[i]都是从st到i的最短距离,但现在这个是做不到的,只能保证是dis
的最短距离。
代码1
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<math.h>
#include<algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
const int maxm=1e5+5;
const ll inf=0x3f3f3f3f3f3f3f3f;
ll dis[maxn];
bool vis[maxn];
int path[maxn];//存前一个节点//
struct node{
int to,cost;
};
vector<node> e[maxn];
void spfa(int st,int n){
mem(dis,inf);
mem(vis,false);
queue<int> que;
dis[st]=0;
que.push(st);
while(!que.empty()){
int now=que.front();
que.pop();
vis[now]=false;
if(dis[now]>dis
) continue;
int size=e[now].size();
for(int i=0;i<size;i++){
int to=e[now][i].to;
int cost=e[now][i].cost;
if(dis[to]>dis[now]+cost){
dis[to]=dis[now]+cost;
path[to]=now;
if(!vis[to]){
vis[to]=true;
que.push(to);
}
}
}
}
}
int main(){
std::ios::sync_with_stdio(false);
int n,m;
scanf("%d %d",&n,&m);
for(int i=0;i<m;i++){
int x,y,cost;
scanf("%d %d %d",&x,&y,&cost);
e[x].push_back((node){y,cost});
e[y].push_back((node){x,cost});
}
spfa(1,n);
int a[maxn];
// cout<<dis
<<endl;
// cout<<inf<<endl;
mem(a,-1);
if(dis
==inf) cout<<-1<<endl;
else{
int k=0;
a[k]=n;
k++;
for(int i=n;i>1;){
int j=path[i];
a[k]=j;
i=j;
k++;
}
for(int i=k-1;i>=0;i--){
cout<<a[i];
if(i>=1) cout<<" ";
else cout<<endl;
}
}
}然后是链式向前星,中间的自主学习推荐一个链接。
理解后同样运用spfa,时间同样被削减到了78ms.要提的是中间re过,原因是用链式向前星的时候。e的数组大小要为m的2倍,因为对于无向图每条边要存2次!
代码2
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<math.h>
#include<algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
const int maxm=1e6+5;
const ll inf=0x3f3f3f3f3f3f3f3f;
ll dis[maxn];
bool vis[maxn];
int path[maxn];//存前一个节点//
ll head[maxn];
struct node{
ll to,cost,next;
}e[2*maxn];
ll cnt=0;
void spfa(ll st){
mem(dis,inf);
mem(vis,false);
queue<ll> que;
dis[st]=0;
que.push(st);
while(!que.empty()){
int now=que.front();
que.pop();
vis[now]=false;
/* int size=e[now].size();
for(int i=0;i<size;i++){
int to=e[now][i].to;
int cost=e[now][i].cost;
if(dis[to]>dis[now]+cost){
dis[to]=dis[now]+cost;
path[to]=now;
if(!vis[to]){
vis[to]=true;
que.push(to);
}
}
}*/
for(ll i=head[now];i!=-1;i=e[i].next){
ll to=e[i].to;
ll cost=e[i].cost;
if(dis[to]>dis[now]+cost){
dis[to]=dis[now]+cost;
path[to]=now;
if(!vis[to]){
vis[to]=true;
que.push(to);
}
}
}
}
}
void add(ll a, ll b , ll c) {
e[cnt].cost=c;
e[cnt].to=b;
e[cnt].next=head[a];
head[a] = cnt++;
}
int main(){
// std::ios::sync_with_stdio(false);
ll n,m;
scanf("%I64d %I64d",&n,&m);
mem(head,-1);
for(ll i=0;i<m;i++){
ll x,y,cost;
scanf("%I64d %I64d %I64d",&x,&y,&cost);
add(x,y,cost);
add(y,x,cost);
}
/* for(int i=0;i<=cnt;i++){
cout<<e[i].to<<" "<<e[i].cost<<" "<<e[i].next<<endl;
}*/
spfa(1);
ll a[maxn];
// cout<<dis
<<endl;
// cout<<inf<<endl;
mem(a,-1);
if(dis
==inf) cout<<-1<<endl;
else{
ll k=0;
a[k]=n;
k++;
for(ll i=n;i>1;){
ll j=path[i];
a[k]=j;
i=j;
k++;
}
for(ll i=k-1;i>=0;i--){
printf("%I64d",a[i]);
if(i>=1)printf(" ");
else printf("\n");
}
}
}
相关文章推荐
- codeforces 20C Dijkstra?
- CodeForces 【20C】Dijkstra?
- 【Codeforces 20C】 Dijkstra?
- Codeforces 20C Dijkstra?
- CodeForces 20C Dijkstra? (最短路)
- CF 20C Dijkstra?
- Codeforces 128B. String(优先队列,模仿dijkstra求最短路)
- CodeForces 20C
- 20c Dijkstra?
- cf 20C Dijkstra?
- CodeForces 715B Complete The Graph(Dijkstra)
- Yaroslav and Time - CodeForces 301B Dijkstra
- CodeForces 601A The Two Routes(dijkstra最短路)——Codeforces Beta Round #333 (Div.1 Div. 2)
- Codeforces 715B Complete The Graph(dijkstra+heap)
- CodeForces - 715 B. Complete The Graph(dijkstra+暴力)
- Codeforces 567E President and Roads (用Dijkstra求最短路条数 + 强连通桥)
- CodeForces - 938D Buy a Ticket [Dijkstra]
- Codeforces Alpha Round #20 (Codeforces format) C. Dijkstra?(裸的dijkstra)
- Codeforces 144D. Missile Silos【dijkstra】
- codeforces 938D Buy a Ticket 有初值的Dijkstra、有趣的题目