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. delphi Post数据到网页

    var http: TIdHttp; sendtoserver: TStringStream; str: string; begin http := TIdHttp.Create(); // 创建 h ...

  2. 我用Django搭网站(3)-表单RSA加密

    之前开发项目时因为种种原因一直使用明文提交,表单直接明文提交非常不安全,只要稍加操作就能轻易获取用户的信息.在众里寻他千百度之后决定使用RSA加密方式,简单可靠. 项目准备 一.安装PyCrypto库 ...

  3. wp面试题

    初级工程师 解释什么是依赖属性,它和以前的属性有什么不同?为什么在WPF会使用它? 什么是样式什么是模板 绑定(Binding )的基础用法 解释这几个类的作用及关系: Visual, UIEleme ...

  4. DES加密与解密MD5加密帮助类

    public class TrialHelper { //默认密钥向量 private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xA ...

  5. HTTP总结

    参考: https://www.cnblogs.com/fuqiang88/p/5956363.html https://www.cnblogs.com/zlingh/p/5887143.html h ...

  6. HTML5新增的表单元素有哪些?

    表单控:color ,  calendar  ,  date ,  datetime, datetime-local,  time, mouth , week, email, url , search ...

  7. SpringMVC中视图解析器

    视图解析器:固定写法直接coppy就行 1.dispatcherServlet-servlet.xml中添加 <!-- 视图解析器InternalResourceViewResolver --& ...

  8. 存入azure table时忽略某个属性

    public class CustomTableEntity : TableEntity { public override IDictionary<string, EntityProperty ...

  9. [译文]Casperjs1.1.0参考文档-快速开始

    快速开始 只要casperjs被正确安装,你就可以开始写你的第一个脚本,你可以使用javascript或者coffiescript编译脚本. 提示: 如果你对javascript不是很熟悉,最好先看专 ...

  10. python pika简单实现RabbitMQ通信

    Windows上安装及启动RabbitMQ https://blog.csdn.net/hzw19920329/article/details/53156015 安装python pika库 pip ...