POJ1847 Tram【Dijkstra+思維】
Tram
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 19612 | Accepted: 7339 |
Description
Tram network in Zagreb consists of a number of intersections and rails connecting some of them. In every intersection there is a switch pointing to the one of the rails going out of the intersection. When the tram enters the intersection it can leave only in the direction the switch is pointing. If the driver wants to go some other way, he/she has to manually change the switch.
When a driver has do drive from intersection A to the intersection B he/she tries to choose the route that will minimize the number of times he/she will have to change the switches manually.
Write a program that will calculate the minimal number of switch changes necessary to travel from intersection A to intersection B.
Input
The first line of the input contains integers N, A and B, separated by a single blank character, 2 <= N <= 100, 1 <= A, B <= N, N is the number of intersections in the network, and intersections are numbered from 1 to N.
Each of the following N lines contain a sequence of integers separated by a single blank character. First number in the i-th line, Ki (0 <= Ki <= N-1), represents the number of rails going out of the i-th intersection. Next Ki numbers represents the intersections directly connected to the i-th intersection.Switch in the i-th intersection is initially pointing in the direction of the first intersection listed.
Output
The first and only line of the output should contain the target minimal number. If there is no route from A to B the line should contain the integer "-1".
Sample Input
3 2 1
2 2 3
2 3 1
2 1 2
Sample Output
0
Source
Croatia OI 2002 Regional - Juniors
問題連結:POJ1847 Tram
問題描述:有軌電車軌道有N個交匯點(從1開始編號),交匯點是由整數K及其後K個整數描述,K表示此交匯點可通往的K個交匯點,其後的K個整數表示可通向的交匯點的編號,一次只能通向一個交匯點,第一個數表示初始通向的交匯點,如果司機想要去其他交匯點就必須手動切換一次,問從A到B最少轉換的次數,如果無法達就輸出-1
解題思路:問題的關鍵是如何建邊,邊的權值只有1和0兩種情況,0是交匯點初始通向的初始交匯點的邊,1是剩下的其他軌道。建邊完成後就可以使用最短路演算法進行求解,程式使用Dijkstra演算法
AC的C++程式:
#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
const int N=105;
const int INF=0x3f3f3f3f;
int dist[N];
int g[N][N];
bool vis[N];
struct Node{
int u,w;
//Node(){}
Node(int u,int w):u(u),w(w){}
bool operator<(const Node &a)const
{
return w>a.w;
}
};
void dijkstra(int s,int n)
{
memset(dist,INF,sizeof(dist));
memset(vis,false,sizeof(vis));
dist[s]=0;
priority_queue<Node>q;
q.push(Node(s,0));
while(!q.empty()){
Node f=q.top();
q.pop();
int u=f.u;
if(!vis[u]){
vis[u]=true;
for(int i=1;i<=n;i++)
if(!vis[i]&&g[u][i]!=INF&&dist[i]>dist[u]+g[u][i]){
dist[i]=dist[u]+g[u][i];
q.push(Node(i,dist[i]));
}
}
}
}
int main()
{
int n,a,b,k,x;
scanf("%d%d%d",&n,&a,&b);
memset(g,INF,sizeof(g));
for(int i=1;i<=n;i++){
scanf("%d",&k);
for(int j=1;j<=k;j++){
scanf("%d",&x);
g[i][x]=(j==1)?0:1;
}
}
dijkstra(a,n);
if(dist[b]==INF)
printf("-1");
else
printf("%d\n",dist[b]);
return 0;
}
相關文章
- HDU4725 The Shortest Path in Nya Graph【Dijkstra+思維】
- 思維體系---技術思維、業務資料思維、產品思維、複合思維
- 淺析工具思維、產品思維、品牌思維與定位
- 求職思維和招聘思維求職
- 把流量思維變成留量思維
- 框架思維框架
- 極思維
- 黑客思維黑客
- 你是整體思維還是分析思維? - kentbeck
- 計算思維
- 模型思維(01)模型
- 提升思維邏輯—SimpleMind Pro(思維導圖) for Mac/winMac
- 《計算思維史話》思維導圖——持續更新
- 創新思維框架:第一原則思維 - Neil Kakkar框架
- 什麼是產品思維和專案思維? - Shreyas
- 英語思維與物件導向分析思維的關係物件
- 運維工程師思維導圖運維工程師
- Java思維理清思路Java
- HDU – 4811 – Ball (思維)
- typora思維導圖
- 技術思維2
- 前端思維導圖前端
- 程式碼之外系列第一:索證思維與索進思維
- Xmind使用教程:給思維導圖加水印 「Xmind思維導圖2023」
- 思維決定命運,從四個方面理解深度思維的概念
- 思維導圖學 MavenMaven
- 學會故事思維(下)
- 程式設計師思維程式設計師
- B. Numbers Box(思維)
- 英語?思維導圖?
- Bob in Wonderland(思維+構造)
- TypeScript - 一種思維方式TypeScript
- 常用思維導圖工具
- 什麼是Actor思維?
- 觀潮思維導圖
- 前端思維導圖 8前端
- 思維導圖Xmind 2022
- 思維導圖軟體