没有言语可以表达我的生气啦!!!!!!!!!!!!!!!!!!!!!!!!!!!
写了俩遍 , 发布的时候都没有了??!!!!生气!!!
还有!!!裸的Dijkstra样例出成这样!!!!看了老久久了!!!
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
心平气和地重新写一波。。。。。。。
样例解释:
n = 3 (三个顶点 ,三个边 ) a = 2(起点) b = 1(终点)
2 2 3 表示的是有俩个顶点分别是2 和 3 , 第一个是默认的方向 , 操作数为0 , 其他的是需要改变方向的 , 操作数为1
假设第三行是3 1 2 4 表示的是有三个顶点 , 3到1是默认的方向 , 3到2 和3 到4 是需要改变方向的
那么我们可以看成默认的方向的话 ,他们之间的权值为0 , 需要改变方向的 , 他们的权值为1
邻接矩阵存图
#include <stdio.h>
#include <iostream>
#include <cstring>
using namespace std;
#include <vector>
#define INF 0x3f3f3f3f
const int maxn = 1009;
int vis[maxn] , dis[maxn] , data[maxn][maxn] ;
int n , a , b , minn;
void Dijkstra()
{
int u;
for(int i = 1 ; i <= n-1 ; i++)
{
minn = INF;
// u = a;
for(int j = 1 ; j <= n ; j++)
{
if(vis[j] == 0 && dis[j] < minn)
{
minn = dis[j];
u = j;
}
}
vis[u] = 1;
for(int k = 1 ; k <= n ; k++)
{
if(dis[k] > dis[u] + data[u][k])
{
dis[k] = dis[u] + data[u][k];
}
}
}
}
int main()
{
while(~scanf("%d %d %d" , &n , &a , &b))
{
int m , s;
memset(vis , 0 , sizeof(vis));
for(int i = 1 ; i <= n ; i++)
{
for(int j = 1 ; j <= n ; j++)
{
if(i == j)
{
data[i][j] = 0 ;
}
else
{
data[i][j] = INF;
}
}
}
for(int i = 1 ; i <= n ; i++)
{
scanf("%d" , &m);
for(int j = 0 ; j < m ; j++)
{
scanf("%d" , &s);
if(j == 0)
{
data[i][s] = 0;
}
else
{
data[i][s] = 1;
}
}
}
vis[a] = 1;
for(int i = 1 ; i <= n ; i++)
{
dis[i] = data[a][i];
}
dis[a] = 0;
Dijkstra();
if(dis[b] >= INF)
{
printf("-1\n");
}
else
{
printf("%d\n" , dis[b]);
}
}
return 0;
}
vector存图
#include <stdio.h>
#include <iostream>
#include <cstring>
using namespace std;
#include <vector>
#define INF 0x3f3f3f3f
const int maxn = 1009;
int vis[maxn] , dis[maxn] ;
int n , a , b ;
struct node
{
int to ;
int cost;
node(int _to , int _cost):to(_to) , cost(_cost){
}
};
vector<node>V[maxn];
void Dijkstra()
{
int u , minn;
for(int i = 1 ; i <= n-1 ; i++)
{
minn = INF ;
u = a;
for(int j = 1 ; j <= n ; j++)
{
if(vis[j] == 0 && dis[j] < minn)
{
minn = dis[j];
u = j;
}
}
vis[u] =1;
for(int k = 0 ; k < V[u].size() ; k++)
{
int v = V[u][k].to;
int w = V[u][k].cost;
if(dis[v] > dis[u] + w)
{
dis[v] = dis[u] + w;
}
}
}
}
int main()
{
int m , s;
while(~scanf("%d %d %d" , &n , &a , &b ))
{
memset(dis , INF , sizeof(dis));
memset(vis , 0 , sizeof(vis));
for(int i= 0 ; i < maxn ; i++)V[i].clear();
for(int i = 1 ; i <= n ; i++)
{
scanf("%d" , &m);
for(int j = 0 ; j < m ; j++)
{
scanf("%d" , &s);
if(j == 0)
{
V[i].push_back(node(s , 0));
}
else
{
V[i].push_back(node(s , 1));
}
}
}
vis[a] = 1;
for(int i = 0 ; i < V[a].size() ; i++)
{
dis[V[a][i].to] = V[a][i].cost;
}
dis[a] = 0;
Dijkstra();
if(dis[b] == INF)
{
printf("-1\n");
}
else
{
printf("%d\n" , dis[b]);
}
}
return 0;
}