简单题,动态全源对短路~

View Code
#include <cstdio>
#include
<cstring>

const int SIZE = 512;
const int INF = 0x7F7F7F7F;

bool mark[SIZE];
int dist[SIZE][SIZE];
int ans[SIZE][SIZE];

int main()
{
int nn;
int nm;
int nq;

int uu;
int vv;
int val;

int opp;
int cas = 0;

while (scanf("%d %d %d",&nn,&nm,&nq) == 3)
{
if (nn == 0 && nm == 0 && nq == 0) break;

if(cas > 0) printf("\n");

memset(mark,
false,sizeof(mark));
memset(dist,
0x7F,sizeof(dist));
memset(ans,
0x7F,sizeof(ans));

for (int i = 0;i < nn;i++)
dist[i][i]
= ans[i][i] = 0;

for (int i = 0;i < nm;i++)
{
scanf(
"%d %d %d",&uu,&vv,&val);
if (dist[uu][vv] > val)
dist[uu][vv]
= val;
}

printf(
"Case %d:\n",++cas);

for (int i = 0;i < nq;i++)
{
scanf(
"%d",&opp);
if (opp == 0)
{
scanf(
"%d",&uu);
if (mark[uu])
{
printf(
"ERROR! At point %d\n",uu);
continue;
}
mark[uu]
= true;

for (int i = 0;i < nn;i++)
if (mark[i])
{
ans[i][uu]
= dist[i][uu];
ans[uu][i]
= dist[uu][i];
}

for (int i = 0;i < nn;i++)
{
if (!mark[i]) continue;

if (ans[i][uu] != INF)
for (int j = 0;j < nn;j++)
if (mark[j] && ans[j][i] != INF)
if (ans[j][uu] > ans[j][i] + ans[i][uu])
ans[j][uu]
= ans[j][i] + ans[i][uu];
if (ans[uu][i] != INF)
for (int j = 0;j < nn;j++)
if (mark[j] && ans[i][j] != INF)
if (ans[uu][j] > ans[uu][i] + ans[i][j])
ans[uu][j]
= ans[uu][i] + ans[i][j];
}

for (int i = 0;i < nn;i++)
if (ans[i][uu] != INF)
for (int j = 0;j < nn;j++)
if (ans[uu][j] != INF)
if (ans[i][j] > ans[i][uu] + ans[uu][j])
ans[i][j]
= ans[i][uu] + ans[uu][j];
}
else
{
scanf(
"%d %d",&uu,&vv);
if (!mark[uu] || !mark[vv]) printf("ERROR! At path %d to %d\n",uu,vv);
else if (ans[uu][vv] == INF) printf("No such path\n");
else printf("%d\n",ans[uu][vv]);
}
}
}
return 0;
}