题意

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;}

Kattis - detour 题解相关推荐

  1. 集训队每周一赛2020-03-06(思维+暴力)

    第二次周赛 A 我是A题 CodeForces 1305 题解 B 我是B题 计蒜客 A1530 题解 C 我是C题 HDU 2673 题解 D 我是D题 CodeForces 304B 题解 E 我 ...

  2. [JS][dfs]题解 | #迷宫问题#

    题解 | #迷宫问题# 题目链接 迷宫问题 题目描述 定义一个二维数组 N*M ,如 5 × 5 数组下所示: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 1, 1 ...

  3. [JS][dp]题解 | #打家劫舍(一)#

    题解 | #打家劫舍(一)# 题目链接 打家劫舍(一) 题目描述 描述 你是一个经验丰富的小偷,准备偷沿街的一排房间,每个房间都存有一定的现金,为了防止被发现,你不能偷相邻的两家,即,如果偷了第一家, ...

  4. [JS]题解 | #魔法数字#

    题解 | #魔法数字# 题目链接 魔法数字 题目描述 牛妹给牛牛写了一个数字n,然后又给自己写了一个数字m,她希望牛牛能执行最少的操作将他的数字转化成自己的. 操作共有三种,如下: 在当前数字的基础上 ...

  5. [JS]题解 | #岛屿数量#

    题解 | #岛屿数量# 题目链接 岛屿数量 题目描述 时间限制:1秒 空间限制:256M 描述 给一个01矩阵,1代表是陆地,0代表海洋, 如果两个1相邻,那么这两个1属于同一个岛.我们只考虑上下左右 ...

  6. [JS] 题解:提取不重复的整数

    题解:提取不重复的整数 https://www.nowcoder.com/practice/253986e66d114d378ae8de2e6c4577c1 时间限制:1秒 空间限制:32M 描述 输 ...

  7. 洛谷-题解 P2672 【推销员】

    独门思路!链表加优先队列! 这题一望,贪心是跑不掉了,但是我贪心并不好,所以想到了一个复杂一些但思路更保稳的做法 思路: 1 因为是离线操作,所以我们可以倒着求,先求x=n的情况,因为那样直接就知道了 ...

  8. [洛谷1383]高级打字机 题解

    题解 这道题一看就珂以用主席树啊 这是一道神奇的题目,那么我们先敲一个主席树,然后维护一个数组len,表示下一次应该在len + 1插入, 之后对于T操作,在上一个版本的len + 1上直接执行插入 ...

  9. luogu P1549 棋盘问题(2) 题解

    luogu P1549 棋盘问题(2) 题解 题目描述 在\(N * N\)的棋盘上\((1≤N≤10)\),填入\(1,2,-,N^2\)共\(N^2\)个数,使得任意两个相邻的数之和为素数. 例如 ...

最新文章

  1. 2021年大数据Spark(三十五):SparkStreaming数据抽象 DStream
  2. Linux终端运行fasterrcnn,对yolo与fasterrcnn anchors的理解
  3. TCGA收官之作—27篇重磅文献绘制“泛癌图谱”
  4. [Qt教程] 第36篇 网络(六)UDP
  5. .Android开发在Eclipse环境中无法显示提示信息This element neither has attached
  6. 如何把安全证书导入到java中的cacerts证书库(转)
  7. 11.context_suggester
  8. Spring中3种实例Bean的方法及设置Bean的别名
  9. Linux日志服务器的搭建
  10. Dev c++一些调试方法
  11. BERT源码分析PART I
  12. scala 与 spark 并行化
  13. springboot @value 默认值_原创 | 搞定默认值
  14. 多年前,初始架构:php+mysql下,对网站架构方面的一些认识
  15. 微信小程序 集成腾讯云IM做的聊天室
  16. 714 买卖股票的最佳时机含手续费(状态机dp)
  17. 学习深度学习过程中的一些经验与方法
  18. Android打开系统自带文件管理器,全选菜单选项
  19. xls批量转换为xlsx格式文件
  20. MySQL 两张表关联更新(用一个表的数据更新另一个表的数据)两个表使用条件从另外一个表获取数据更新本表

热门文章

  1. python crypto模块详解_crypto
  2. 我们采访了这些游戏大神,这是他们给新人的建议
  3. 大型论坛虚拟服务器,论坛可以用虚拟主机吗
  4. Articulate Presenter文字乱码的排除
  5. java入门提高篇:Day1 抽象类
  6. GPT分区无法安装Windows10的原因与解决方法
  7. ARC 085 NRE
  8. Python脚本爬取天地图瓦片
  9. python turtle什么意思_python模块turtle简单用法
  10. 中证500等主要指数的市盈率(PE)估值高度