题目链接:http://poj.org/problem?id=1847
Tram
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 15332 | Accepted: 5638 |
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.
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.
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题意:火车从出发点到目的地至少要改变开关几次。
思路:floyd裸模板。
由于本人代码习惯不好,犯了一个大错,给大家分享一下。(吓得我以后inf老老实实写成0x3f3f3f3f)
const int inf = 0x3f; (inf=63)
int ma[105][105];
初始化我一般都用memset(ma,inf,sizeof(ma));
初始化后数组里的值都是1061109567,但是数组里的值不等于inf。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int inf = 0x3f;
int ma[105][105];
int mb[105][105];
int mc[105][105];
int md[105][105];
long long me[105][105];
int main()
{
memset(ma,inf,sizeof(ma));
memset(mb,0x3f3f,sizeof(mb));
memset(mc,0x3f3f3f,sizeof(mc));
memset(md,0x3f3f3f3f,sizeof(md));
memset(me,0x3f,sizeof(me));
printf("inf=0x3f =%d ma[0][0]=%d\n",inf,ma[0][0]);
printf("0x3f3f =%d mb[0][0]=%d\n",0x3f3f,mb[0][0]);
printf("0x3f3f3f =%d mc[0][0]=%d\n",0x3f3f3f,mc[0][0]);
printf("0x3f3f3f3f =%d md[0][0]=%d\n",0x3f3f3f3f,md[0][0]);
printf("%I64d %I64d\n",0x3f3f3f3f3f3f3f3f,me[0][0]);
memset(mb,0x3f3f3f3f3f3f3f3f3f3f3f3f,sizeof(mb));
printf("%d\n",mb[0][0]);
return 0;
}
得出一个结论:初始化时0x3f都会把他初始化为极限值的一半(跟后面3f的数量无关(至少一个))
http://blog.csdn.net/Vmurder/article/details/46537613 这篇博客里面有int long long double float 的极大值,较大值,极小值,较小值,int 和long long的可以记一下,double,float用的比较少。
最后附上AC代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
#include<string>
#define LL long long
#define eps 1e-8
#define N 30005
#define M 100005
using namespace std;
const int mod = 10000007;
const int inf = 0x3f3f3f3f;
const int maxx = 1e6 +10;
int n,a,b,k,m;
int ma[105][105];
void floyd()
{
for(int k=1; k<=n; k++)
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
if(ma[i][j]>ma[i][k]+ma[k][j])
ma[i][j]=ma[i][k]+ma[k][j];
if(ma[a][b]==inf)//这个地方因为代码习惯不好,wa了好多次0.0
printf("-1\n");
else
printf("%d\n",ma[a][b]);
}
int main()
{
while(~scanf("%d%d%d",&n,&a,&b))
{
memset(ma,inf,sizeof(ma));
for(int i=1; i<=n; i++)
{
scanf("%d",&k);
for(int j=0; j<k; j++)
{
scanf("%d",&m);
ma[i][m]=j>0;//j>0:是一个表达式,如果为真,则表达式的值为1,是假则为0;
}
}
floyd();
}
}