Kattis - detour 题解

题意

After last year’s edition of the BAPC, you are still stuck in Delft. In order to participate again this year, you are going to Amsterdam by bus. During the journey you look out of the window and look for traffic signs that point in the direction of Amsterdam. To your surprise, you notice that the bus is never taking the roads that are pointed out by the signs!
You think that the bus company might have 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 this is possible. Can you figure out whether there exists a bus route that 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 is placed at every intersection. You may assume that the input graph is both simple and connected, and that there is a unique optimal route to Amsterdam from every intersection.
Input
A single line containing two integers: n (2≤n≤1e5), the number of intersections, and m (1≤m≤1e6), the number of undirected roads that connect the intersections. The intersections are numbered from 0 to n−1
. Delft is denoted by intersection i=0 and Amsterdam is denoted by intersection i=1.m lines that specify the roadsA road is specified by three integers, ai, bi (0≤ai,bi<n and ai≠bi) and di (0≤di≤500000), where ai and bi
are ids of the two intersections that are connected by this road and di is the distance that the bus has 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 that satisfies the requirements, in case such a path exists.
A path is specified by a single line containing an integer kk, the length of the path, followed by k integers pi that specify the intersections along the path in the order in which they are crossed, with p0=0 and pk−1=1
The text “impossible”, if such a path does not exist.

题解

堆优化的Dijkstra算法求出以1节点为起点的最短路,然后去除所有在最短路包括的边,对于剩下的边dfs找到一条通路,不能找到则输出impossible

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+7;
typedef long long ll;
const int INF=0x3f3f3f3f;
struct node{
	int s,t,c,ne;
} ;
node edge[maxn];
int tot;
int dis[maxn];
int head[maxn],pre[maxn];
bool vis[maxn];
bool f;
int ans[maxn],len=0;
struct nod{
	int t,c;
	nod(int _t=0,int _c=0):t(_t),c(_c) {}
    bool operator <(const nod &r)const
    {
        return c>r.c;
    }
};
void addedge(int s,int t,int cost)
{
    edge[tot].s=s;
    edge[tot].t=t;
    edge[tot].c=cost;
    edge[tot].ne=head[s];
    head[s]=tot++;
}
void Dj(int n,int start){
	priority_queue<nod> q;
	while(!q.empty()) q.pop();
	dis[start]=0;
	q.push(nod(start,0));
	nod tmp;
	while(!q.empty()){
		tmp=q.top();
		q.pop();
		int u=tmp.t;
		if(vis[u]) continue;
		vis[u]=1;
		for(int i=head[u];i!=-1;i=edge[i].ne){
			int v=edge[i].t;
			int c=edge[i].c;
			if(!vis[v]&&dis[v]>dis[u]+c){
				pre[v]=u;
				dis[v]=dis[u]+c;
				q.push(nod(v,dis[v]));
			}
		}
		
	}
}
void print_ans(int point)
{
    if(point==1)
    {
        cout<<" "<<0;
        return ;
    }
    print_ans(point-1);
    cout<<" "<<ans[point]-1;
}
int n,m;
void dfs(int t,int p){
	ans[++len]=t;
	vis[t]=1;
	if(t==2){
		f=1;
		cout<<len;
		print_ans(len);
		cout<<endl;
		return ;
	}
	for(int i=head[t];i!=-1;i=edge[i].ne){
		int v=edge[i].t;
		if(pre[t]==v||vis[v]) continue;
		dfs(v,t);
		if(f) return ;
		
	}
	len--;
}
int main(){
	
		 
    memset(pre,-1,sizeof(pre));
    memset(vis,0,sizeof(vis));
    memset(dis,INF,sizeof(dis));
    memset(head,-1,sizeof(head));
    f=tot=0;
    cin>>n>>m;
    for(int i=1,s,t,c;i<=m;i++){
    	cin>>s>>t>>c;
    	addedge(s+1,t+1,c);
    	addedge(t+1,s+1,c);
	}
	Dj(n,2);
	memset(vis,0,sizeof(vis));
    dfs(1,-1);
    if(!f) cout<<"impossible"<<endl;
    return 0;
    
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值