TOJ 4523 Transportation
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
先要对图进行压缩。因为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的更多相关文章
- TOJ 3744 Transportation Costs
描述 Minya Konka decided to go to Fuzhou to participate in the ACM regional contest at their own expen ...
- POJ 1797 Heavy Transportation(最大生成树/最短路变形)
传送门 Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 31882 Accept ...
- TOJ 2776 CD Making
TOJ 2776题目链接http://acm.tju.edu.cn/toj/showp2776.html 这题其实就是考虑的周全性... 贡献了好几次WA, 后来想了半天才知道哪里有遗漏.最大的问题 ...
- 【HDU 4940】Destroy Transportation system(无源无汇带上下界可行流)
Description Tom is a commander, his task is destroying his enemy’s transportation system. Let’s repr ...
- Heavy Transportation(最短路 + dp)
Heavy Transportation Time Limit:3000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64 ...
- POJ 1797 Heavy Transportation (Dijkstra变形)
F - Heavy Transportation Time Limit:3000MS Memory Limit:30000KB 64bit IO Format:%I64d & ...
- poj 1797 Heavy Transportation(最短路径Dijkdtra)
Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 26968 Accepted: ...
- POJ 1797 Heavy Transportation
题目链接:http://poj.org/problem?id=1797 Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K T ...
- uva301 - Transportation
Transportation Ruratania is just entering capitalism and is establishing new enterprising activiti ...
随机推荐
- React+gulp+browserify模块化开发
阅读本文需要有React的基础知识,可以在React 入门实例教程和React中文官网进行基础学习. 没有React基础也可以学习本文,本文主要不是学习React,而是gulp+browserify进 ...
- angular formBuilder
- Android动态显示或隐藏密码框中的密码(Android学习笔记)
activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&qu ...
- Response Assertion(响应断言)
响应断言可以让你添加匹配字符串来匹配请求和响应的各个字符串. 匹配字符串可以是1.Contains和Matches正则表达式:2.Equals和SubString文本类型,大小写敏感. Apply t ...
- jsonp的原理及其使用
原理: 1.创建script标签 2.src远程地址 3.返回的数据必须为js格式 1.因为浏览器处于安全原因不允许跨域请求,但是允许跨域倒入js文件,所以需要创建script标签 2.src远程地址 ...
- JSP标签的用法
JSP动作标签: 通过动作标签,程序员可以在JSP页面中把页面的显示功能部分 封装起来,是整个页面更简洁和易于维护 <jsp:useBean> 装载一个将在JSP页面中使用的JavaBea ...
- [国家集训队]部落战争 最大流 BZOJ2150
题目描述 lanzerb的部落在A国的上部,他们不满天寒地冻的环境,于是准备向A国的下部征战来获得更大的领土. A国是一个M*N的矩阵,其中某些地方是城镇,某些地方是高山深涧无人居住.lanzerb把 ...
- bootstrap的其他
情境文本颜色 <p class="text-muted">...</p> <p class="text-primary">. ...
- Domoticz 接入苹果的 HomeKit 实现 Siri 控制
前言 接上次的折腾,这次尝试将 Domoticz 接入到苹果的 HomeKit,也就是在 iPhone 的 Siri 中可以语音控制.参考官方文档 步骤 安装 nodejs curl -sL http ...
- n阶幻方
前序 最近在学习一些经典的算法,搞得头昏脑涨,就想换换脑子.在家里的旧书堆里面乱翻,无意中将一本具有十多年历史的小学数学奥林匹克竞赛的书发掘了出来,能放到现在挺不容易的,就拿起来随便翻翻.看了看目录, ...