Description

Given N stations, you want to carry goods from station 1 to station N. Among these stations, we use M tubes to connect some of them. Each tube can directly connect K stations with each other. What is the minimum number of stations to pass through to carry goods from station 1 to station N?

Input

The first line has three positive integers: N (1 ≤ N ≤ 100 000, K (1 ≤ K ≤ 1 000) and M (1 ≤ M ≤ 1 000).
Then follows M lines, each line has K positive integers describing the connected stations to this tube.

Output

Output  the minimum number of stations.

If it cannot carry goods from station 1 to station N, just output -1.

Sample Input

9 3 5
1 2 3
1 4 5
3 6 7
5 6 7
6 8 9

Sample Output

4

Source

TOJ

先要对图进行压缩。因为K点两两相连,则表示可以引入一个虚拟的点(N+i)这些点到的虚拟的点距离都相等。

然后进行广搜一开始想到的是SPFA这种方法。不过对于后来直接广搜就莫名奇妙的过了。

于是去问贞贞这是为什么呢?

后来想明白了,因为点点之间的距离是等距的。如果有条路到N的距离比较长的话,那么一定是晚点找到的。

所以先返回的一定是最短的。

 #include <stdio.h>
#include <iostream>
#include <queue>
#define inf 0x3f3f3f3f
using namespace std; int N,K,M;
int dist[];
int visited[];
vector<int> V[]; int bfs(){
queue<int> Q;
for(int i=; i<=N+M; i++){
dist[i]=inf;
visited[i]=;
}
dist[]=;
visited[]=;
Q.push();
while( !Q.empty() ){
int u=Q.front();
if(u==N)return dist[u];
Q.pop();
for(int i=; i<V[u].size(); i++){
int v=V[u][i];
if(!visited[v]){
if(v<=N)
dist[v]=dist[u]+;
else
dist[v]=dist[u];
Q.push(v);
visited[v]=;
}
}
}
return -;
} int main()
{
while( scanf("%d %d %d" ,&N ,&K ,&M)!=EOF ){
for(int i=; i<=M; i++){
for(int j=; j<K; j++){
int x;
scanf("%d" ,&x);
V[N+i].push_back(x);
V[x].push_back(N+i);
}
}
int ans=bfs();
printf("%d\n",ans);
}
return ;
}

TOJ 4523 Transportation的更多相关文章

  1. TOJ 3744 Transportation Costs

    描述 Minya Konka decided to go to Fuzhou to participate in the ACM regional contest at their own expen ...

  2. POJ 1797 Heavy Transportation(最大生成树/最短路变形)

    传送门 Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 31882   Accept ...

  3. TOJ 2776 CD Making

    TOJ 2776题目链接http://acm.tju.edu.cn/toj/showp2776.html 这题其实就是考虑的周全性...  贡献了好几次WA, 后来想了半天才知道哪里有遗漏.最大的问题 ...

  4. 【HDU 4940】Destroy Transportation system(无源无汇带上下界可行流)

    Description Tom is a commander, his task is destroying his enemy’s transportation system. Let’s repr ...

  5. Heavy Transportation(最短路 + dp)

    Heavy Transportation Time Limit:3000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64 ...

  6. POJ 1797 Heavy Transportation (Dijkstra变形)

    F - Heavy Transportation Time Limit:3000MS     Memory Limit:30000KB     64bit IO Format:%I64d & ...

  7. poj 1797 Heavy Transportation(最短路径Dijkdtra)

    Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 26968   Accepted: ...

  8. POJ 1797 Heavy Transportation

    题目链接:http://poj.org/problem?id=1797 Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K T ...

  9. uva301 - Transportation

      Transportation Ruratania is just entering capitalism and is establishing new enterprising activiti ...

随机推荐

  1. Math对象的使用

    1. Math.floor() === 向下取整 2.Math.random() === 取一个浮点随机数 3.Math.round() === 四舍五入后一个最接近的整数 4.Math.ceil() ...

  2. HTML <area> 标签区域map标签

    1.距形:(左上角顶点坐标为(x1,y1),右下角顶点坐标为(x2,y2)) <area shape="rect" coords="x1,y1,x2,y2" ...

  3. angular 模板表单

  4. C# 调接口

    上一个项目,需要mvc管理后台调接口项目,以便后期的重构扩展,调研后发现后台用的ajax请求,直接调接口可能会有跨域问题,最终在c#代码中实现了这个需求. 1,Ajax请求后台 将接口所需参数传入 2 ...

  5. 汇编工具安装二:RadASM的安装!

    已经配置好的汇编工具下载地址:http://download.csdn.net/detail/sunylat/9189543 RadASM也是一款汇编开发工具,网址:http://www.oby.ro ...

  6. 主流C语言编译器介绍

  7. f(!gotop.length) return false;

    if (!$("p").length) return; if语句接收一个布尔值,如果布尔值为true则执行接下来的语句,布尔值为false则执行else关键词后的语句. JavaS ...

  8. Nginx 进行性能配置

    总所周知,网络上我们购买的服务器的性能各不相同,如果采用 Nginx 的默认配置的话,无法将服务器的全部性能优势发挥出来,我们应该选择适合自己需求的配置. 当我们默认安装后 Nginx 后,我们便得到 ...

  9. node创建一个简单的web服务

    本文将如何用node创建一个简单的web服务,过程也很简单呢~ 开始之前要先安装node.js 1.创建一个最简单的服务 // server.js const http = require('http ...

  10. 三元运算符,i++(先用后加) ++i (先加后用)区别

    三元运算符是软件编程中的一个固定格式,语法是“条件表达式?表达式1:表达式2”.使用这个算法可以使调用数据时逐级筛选. 表达式:“()? :”. ()中进行二元运算 ?在运算,就形成三元运算符   i ...