又犯了同样的错误,开edge数组开小了,应该是maxm,开成了maxn
正反两个图,进行两次dijkstra即可
View Code
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
#define maxn 1005
#define maxm 100005
#define inf 1000000000
struct Edge
{
int v, w, next;
}edge[maxm], opedge[maxm];
int n, m, t, head[maxn], ophead[maxn];
int tot, optot, len[maxn], oplen[maxn];
void init()
{
tot = 0;
optot = 0;
memset(head, -1, sizeof(head));
memset(ophead, -1, sizeof(ophead));
scanf("%d%d%d", &n, &m, &t);
t--;
for (int i = 0; i < m; i++)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
a--;
b--;
edge[tot].next = head[a];
head[a] = tot;
edge[tot].v = b;
edge[tot].w = c;
tot++;
opedge[optot].next = ophead[b];
ophead[b] = optot;
opedge[optot].v = a;
opedge[optot].w = c;
optot++;
}
}
void dijkstra(int head[], Edge edge[], int len[])
{
bool visited[maxn];
memset(visited, 0, sizeof(visited));
len[t] = 0;
while (1)
{
int best = inf, besti = -1;
for (int i = 0; i < n; i++)
{
if (len[i] < best && !visited[i])
{
best = len[i];
besti = i;
}
}
if (besti == -1)
break;
visited[besti] = true;
for (int i = head[besti]; i != -1; i = edge[i].next)
{
int v = edge[i].v;
int w = edge[i].w;
if (len[v] > len[besti] + w)
len[v] = len[besti] + w;
}
}
}
int main()
{
//freopen("D:\\t.txt", "r", stdin);
init();
for (int i = 0; i < n; i++)
{
len[i] = oplen[i] = inf;
}
dijkstra(head, edge, len);
dijkstra(ophead, opedge, oplen);
int best = 0;
for (int i = 0; i < n; i++)
if (best < len[i] + oplen[i])
best = len[i] + oplen[i];
printf("%d\n", best);
return 0;
}