poj1847 Tram SPFA
来源:互联网 发布:淘宝店铺交易指数 编辑:程序博客网 时间:2024/06/08 15:39
题意:
火车从s开到t,其中有很多岔路口,每个岔路口都能到达一些点,默认到达的点为该行读入的第一个数字。
每个岔路口都可以改变前进的方向,每改变一次代价为1(对于每个岔路口,只要改变了方向,不论改变到哪里代价都是1)。
问从s到t最少的代价是多少。
思路:
每个节点与所有可到达节点之间连边,与初始指向节点的权值为0,与其余可到达的节点的权值为1。
然后求最短路。
代码(704K,0MS):
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#include <vector>#include <queue>using namespace std;struct Edge{int to, v;Edge(int a, int b) : to(a), v(b) {}};int n, s, t;vector<Edge> edges[105];int dist[105];int vis[105];int spfa() {queue<int> q;memset(dist, 0x3f, sizeof(dist));memset(vis, 0, sizeof(vis));dist[s] = 0;vis[s] = 1;q.push(s);while (!q.empty()) {int cur = q.front();q.pop();for (int i = 0; i < edges[cur].size(); i++) {Edge e = edges[cur][i];if (dist[cur] + e.v < dist[e.to]) {dist[e.to] = dist[cur] + e.v;if (!vis[e.to]) {vis[e.to] = 1;q.push(e.to);}}}vis[cur] = 0;}return dist[t];}int main() {while (~scanf("%d %d %d", &n, &s, &t)) {int cnt, a;for (int i = 1; i <= n; i++) {scanf("%d", &cnt);for (int j = 0; j < cnt; j++) {scanf("%d", &a);if (!j) edges[i].push_back(Edge(a, 0));else edges[i].push_back(Edge(a, 1));}}int ans = spfa();if (ans == 0x3f3f3f3f) ans = -1;printf("%d\n", ans);}return 0;}
0 0
- poj1847 Tram SPFA
- POJ1847--Tram 【最短路spfa】
- POJ1847:Tram
- POJ1847---Tram
- [poj1847] Tram
- poj1847 Tram
- Tram(poj1847,floyd)
- poj1847 Tram(最短路)
- POJ1847 Tram(最短路径)
- POJ1847 Tram最短路径 Dijkstra
- poj1847——Tram(dijkstra)
- POJ_P1847 Tram(SPFA)
- Tram [最短路][spfa]
- POJ 1847 Tram(spfa)
- poj1847 - Tram (最短路径问题) (Dijkstra)
- POJ1847 Tram ACM解题报告(dijkstra求最短路)
- 【最短路】poj1847 SPFA或Floyd
- poj1847
- 编译安装Hadoop
- 进程管理
- 基于annotation的hibernate主键生成策略
- Ueditor(二):添加字体
- 重构(Refactoring)技巧 读书笔记
- poj1847 Tram SPFA
- MySQL NOLOCK 事务级别
- enq: TX - row lock contention 等待事件
- 用javascript与java进行RSA加密与解密
- google 访问host文件配置
- 手机web——自适应网页设计(html/css控制)
- Unsupported major.minor version 51.0解决办法
- spring MVC 错误The request sent by the client was syntactically incorrect ()的解决
- linux下的同步与互斥