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. canvas基本绘制图形

    canvas H5新增的元素,提供了强大的图形的绘制,变换,图片,视频的处理等等.需要使用JavaScript脚本操作 浏览器支持 大多数的现代浏览器都可以支持:IE8以下的浏览器不支持 画布 可支持 ...

  2. MVC页面加载会多次请求后台问题

    最近调试代码的时候发现有些控制器有代码走两遍的情况,后台发现是前端url或者herf标签导致请求了mvc路由,具体案例如下: 这两种路径为空的时候都会导致请求mvc路由重复请求后台方法

  3. sql server重建系统数据库

    方法一:https://bbs.csdn.net/topics/100013082 方法二:http://blog.51cto.com/jimshu/1095780 *** 方法三:https://b ...

  4. windows下go调用内存dll

    有时候我们希望将dll嵌入到程序内部,以提高程序的安全性,这里我写的一个开源memorydll模块. 首先 go get github.com/nkbai/go-memorydll 然后在需要的时候 ...

  5. 201621123012 《Java程序设计》第6周学习总结

    1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图或相关笔记,对面向对象思想进行一个总结. 注1:关键词与内容不求多,但概念之间的联系要清晰 ...

  6. 可变大小、颜色边框、样式的UISwitch

    1.CHSwitch.h // // 文 件 名:CHSwitch.h // // 版权所有:Copyright © 2018 lelight. All rights reserved. // 创 建 ...

  7. 【bzoj3670】: [Noi2014]动物园 字符串-kmp-倍增

    [bzoj3670]: [Noi2014]动物园 一开始想的是按照kmp把fail算出来的同时就可以递推求出第i位要f次可以跳到-1 然后把从x=i开始顺着fail走,走到fail[x]*2<i ...

  8. 利用百度翻译API批量翻译文本

    有时间再做总结! # coding=utf-8 #authority:bing #2017-7-18 import httplib import md5 import urllib import ur ...

  9. webstorm激活服务器地址

    2017.1.4版本可用 http://idea.imsxm.com/

  10. Python3入门之软件安装

    获得更多资料欢迎进入我的网站或者 csdn或者博客园 最近将我的Python笔记整理下,希望能对他人有所帮助,这是第一课,安装入门篇: windows下安装Python 1.下载,从这里下载:[下载链 ...