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 ...
随机推荐
- 挂载ISO 和 KILL 掉占用进程
mount -t iso9660 -o loop,user VMware-tools-linux-8.6.0-425873.iso /mnt/cdrom fuser -m -v -i -k /mnt ...
- LSI SAS3008 RAID配置方法
7.1 概述 LSI SAS3008 RAID 控制卡(以下简称LSI SAS3008)是基于Fusion-MPT™ (消息传递技术)架构的8端口12Gbit/s SAS控制器,并采用PCIe3.0 ...
- SpringMVC+Hibernate 使用 session.update(obj) 未更新的问题
1.使用spring控制事务 2.使用session.update(obj)执行更新 spring事务配置: <bean id="transactionBese" class ...
- C# 密封(2)
上一章节说到 sealed 作用于类,那么sealed 作用到方法和成员上面该如何呢. 在C# 中 Sealed作用于方法必须是重写之后的方法.也就是override+sealed.在之后别的类在继 ...
- 857. Minimum Cost to Hire K Workers
There are N workers. The i-th worker has a quality[i] and a minimum wage expectation wage[i]. Now w ...
- IIS发布的网页上传文件被拒绝
在IIS所在的服务器共享的权限(如下图示,但注意不是加everyone)和共享文件夹的权限里都加上IIS_USER完全控制,如果不行再加上NETWORK SERVICE权限
- python 安装虚拟环境步骤
1.python3.6.3 注: 1.安装的时候,装上你的pip 2.安装的时候,把环境变量记得勾选 3.如果你手动更改安装位置,更改到随意的盘根目录下 2.MySQL pycharm最好安 ...
- Java面向对象之关键字super 入门实例
一.基础概念 (一)super关键字 super关键字的用法和this相似.this代表的是当前对象.super代表的是父类中内存空间. 子父类中是不会出现同名属性的情况. (二)继承中.成员变量问题 ...
- 【FAQ】Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServlet
原因: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spr ...
- 一个简单的Samba服务
上次给大家认识了下,搭建一个服务大概的一个认识. 这次给大家搭建一个Samba服务认识下. 项目准备: 虚拟机一个(Centos6.5版本) 项目目标: 进行samba最简单的配置 项目难度: ❤❤ ...