-
我实在是找不到dijkstra的写法哪里错了,哭了,样例还有自己编的数据都没问题,但是就是WA,放弃 = =
-
第二天到了我终于AC了,问题出在哪里了呢?
WA的代码中,开数组的时候,maxn=250。AC的代码中,max=110。
就是这么神奇,我也不知道为什么,哭。。。
别人的代码
dijkstra:
大佬代码1
大佬代码2
floyed:
大佬代码3
Dijkstra算法:
AC的代码:
#pragma warning(disable:4996)
#include<iostream>
#include<string>
#include<cmath>
#include<ctype.h>
#include<memory.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<iomanip>
#include<set>
#include<list>
#include<vector>
#include<stack>
#include<queue>
#define ll long long int
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 110;
int n, a, b;
int dis[maxn];
int vis[maxn];
int Map[maxn][maxn];
void dijkstra(int s)
{
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= n; i++)
dis[i] = Map[s][i];
/*cout << "dis1:";
for (int k = 0; k <= n; k++)
cout << dis[k] << " ";
cout << endl;*/
//dis[s] = 0;
vis[s] = 1;
for (int i = 1; i <= n; i++)
{
int Min = INF;
int min_index = -1;
for (int j = 1; j <= n; j++)
if (dis[j] < Min && !vis[j])
{
Min = dis[j];
min_index = j;
}
if (min_index != -1)
vis[min_index] = 1;
for (int j = 1; j <= n; j++)
if (dis[j] > dis[min_index] + Map[min_index][j] && !vis[j] && Map[min_index][j] < INF)
dis[j] = dis[min_index] + Map[min_index][j];
}
}
int main()
{
//cin >> n >> a >> b;
while (cin >> n)
{
cin >> a >> b;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
Map[i][j] = i == j ? 0 : INF;
for (int i = 1; i <= n; i++)
{
int num, x;
cin >> num;
for (int j = 1; j <= num; j++)
{
cin >> x;
int cishu;
if (j == 1)//因为初始都是指向第一个岔道编号的的,所以如果现在读入的这个岔口就是第一个岔口的话,不需要旋转,旋转次数为0
cishu = 0;//刚开始时候理解错题意了,判断条件写成了if(x == 1),WA了好多次。。。
else
cishu = 1;
Map[i][x] = cishu;
}
}
dijkstra(a);
if (dis[b] >= INF)
cout << -1 << endl;
else
cout << dis[b] << endl;
/*cout << "dis:";
for (int k = 0; k <= n; k++)
cout << dis[k] << " ";
cout << endl;
cout << "vis:";
for (int k = 0; k <= n; k++)
cout << vis[k] << " ";
cout << endl;*/
}
return 0;
}
WA的代码:
#pragma warning(disable:4996)
#include<iostream>
#include<string>
#include<cmath>
#include<ctype.h>
#include<memory.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<iomanip>
#include<set>
#include<list>
#include<vector>
#include<stack>
#include<queue>
#define ll long long int
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 250;
int n, a, b;
int dis[maxn];
int vis[maxn];
int Map[maxn][maxn];
void dijkstra(int s)
{
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= n; i++)
dis[i] = Map[s][i];
/*cout << "dis1:";
for (int k = 0; k <= n; k++)
cout << dis[k] << " ";
cout << endl;*/
//dis[s] = 0;
vis[s] = 1;
for (int i = 1; i <= n; i++)
{
int Min = INF;
int min_index = -1;
for (int j = 1; j <= n; j++)
if (dis[j] < Min && !vis[j])
{
Min = dis[j];
min_index = j;
}
if (min_index != -1)
vis[min_index] = 1;
for (int j = 1; j <= n; j++)
if (dis[j] > dis[min_index] + Map[min_index][j] && !vis[j] && Map[min_index][j] < INF)
dis[j] = dis[min_index] + Map[min_index][j];
}
}
int main()
{
//cin >> n >> a >> b;
while (cin >> n)
{
cin >> a >> b;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
Map[i][j] = i == j ? 0 : INF;
for (int i = 1; i <= n; i++)
{
int num, x;
cin >> num;
for (int j = 1; j <= num; j++)
{
cin >> x;
int cishu;
if (j == 1)//因为初始都是指向第一个岔道编号的的,所以如果现在读入的这个岔口就是第一个岔口的话,不需要旋转,旋转次数为0
cishu = 0;//刚开始时候理解错题意了,判断条件写成了if(x == 1),WA了好多次。。。
else
cishu = 1;
Map[i][x] = cishu;
}
}
dijkstra(a);
if (dis[b] >= INF)
cout << -1 << endl;
else
cout << dis[b] << endl;
/*cout << "dis:";
for (int k = 0; k <= n; k++)
cout << dis[k] << " ";
cout << endl;
cout << "vis:";
for (int k = 0; k <= n; k++)
cout << vis[k] << " ";
cout << endl;*/
}
return 0;
}
Floyed代码:
但是 Floyed开大了也不会WA(挠头.jpg)
#pragma warning(disable:4996)
#include<iostream>
#include<string>
#include<cmath>
#include<ctype.h>
#include<memory.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<iomanip>
#include<set>
#include<list>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define ll long long int
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 250;
int Map[maxn][maxn];
int main()
{
int n, a, b;
while (cin >> n >> a >> b)
{
//Initialize
for (int i = 0; i <= n; i++)
for (int j = 0; j <= n; j++)
Map[i][j] = i == j ? 0 : INF;
//Input
for (int i = 1; i <= n; i++)
{
int num, x;
cin >> num;
for (int j = 1; j <= num; j++)
{
cin >> x;
if (j == 1)
Map[i][x] = 0;//Map[i][j]含义同Dijkjstra代码
else
Map[i][x] = 1;
}
}
//Floyed
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (Map[i][j] > Map[i][k] + Map[k][j])
Map[i][j] = Map[i][k] + Map[k][j];
//Output
if (Map[a][b] >= INF)
cout << -1 << endl;
else
cout << Map[a][b] << endl;
}
return 0;
}