hdu 3631 Shortest Path(Floyd)

#include<stdio.h>
#include<algorithm>
#include<string.h>
const int N=305;
using namespace std;

int main()
{
    int cas=0,n,m,q,i,j,k,x,y,t,g[305][305],vis[305],inf=1<<30;
    while(~scanf("%d%d%d",&n,&m,&q))
    {
        if(n==0) break;
        if(cas) printf("\n");
        memset(vis,0,sizeof(vis));
        for(i=0; i<N; i++)
            for(j=0; j<N; j++)
                if(i==j) g[i][j]=0;
                else g[i][j]=inf;
        for(i=0;i<m;i++)
        {
            scanf("%d%d%d",&x,&y,&t);
            if(g[x][y]>t) g[x][y]=t;
        }
        printf("Case %d:\n",++cas);
        for(k=0;k<q;k++)
        {
            scanf("%d",&t);
            if(t)
            {
                scanf("%d%d",&x,&y);
                if(vis[x]==0||vis[y]==0) printf("ERROR! At path %d to %d\n",x,y);
                else if(g[x][y]>=inf) printf("No such path\n");
                else printf("%d\n",g[x][y]);
            }
            else
            {
                scanf("%d",&x);
                if(vis[x])
                {
                    printf("ERROR! At point %d\n",x);
                }
                else
                {
                    vis[x]=1;
                    for(i=0;i<N;i++)
                        for(j=0;j<N;j++)
                    {
                        if(g[i][x]>=inf||g[x][j]>=inf) continue;
                        g[i][j]=min(g[i][j],g[i][x]+g[x][j]);
                    }
                }
            }
        }
    }
    return 0;
}

错误代码

/* *********************************************** Author :xryz Email :[email protected] Created Time :6-10 22:57:56 File Name :a.cpp ************************************************ */

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

const int N=300+5;
const int inf=1<<20;
int edge[N][N],vis[N],use[N],n,m,q,dist[N];

int Dijkstra(int v0)
{
    int i,j,k,minx,u;
    memset(vis,0,sizeof(vis));

    for(i=0; i<n; i++)
    {
        if(use[i]&&edge[v0][i]<inf) dist[i]=edge[v0][i];
        else dist[i]=inf;
    }
    dist[v0]=0;
    vis[v0]=1;

    for(i=0; i<n-1; i++)
    {
        minx=inf,u=v0;
        for(j=0;j<n;j++)
        {
            if(!vis[j]&&dist[j]<minx&&use[i])
            {
                minx=dist[j];
                u=j;
            }
        }
        vis[u]=1;
        for(k=0;k<n;k++)
        {
            if(!vis[k]&&edge[u][k]<inf&&dist[k]>dist[u]+edge[u][k]&&use[k])
                dist[k]=dist[u]+edge[u][k];
        }
    }
}

int main()
{
    int x,y,c,i,j,cas=0;
    //freopen("in.txt","r",stdin);
    while(~scanf("%d%d%d",&n,&m,&q)&&n&&m&&q)
    {
        memset(use,0,sizeof(use));
        memset(edge,0,sizeof(edge));
        for(i=0; i<m; i++)
        {
            scanf("%d%d%d",&x,&y,&c);
            if(edge[x][y]==0||edge[x][y]>c)
                edge[x][y]=c;
        }

        for(i=0; i<n; i++)
            for(j=0; j<n; j++)
            {
                if(i==j) edge[i][j]=0;
                else if(edge[i][j]==0) edge[i][j]=inf;
            }

        if(cas) printf("\n");
        printf("Case %d:\n",cas++);
        for(int k=0; k<q; k++)
        {
            scanf("%d",&c);
            if(c==0)
            {
                scanf("%d",&x);
                if(use[x]==1) printf("ERROR! At point %d\n",x);
                else use[x]=1;
            }
            else
            {
                scanf("%d%d",&x,&y);
                if(use[x]==0||use[y]==0)
                    printf("ERROR! At path %d to %d\n",x,y);
                else
                {
                    if(x==y) printf("0\n");
                    else
                    {
                        Dijkstra(x);
                        if(dist[y]<inf) printf("%d\n",dist[y]);
                        else printf("No such path\n");
                    }
                }
            }
        }
    }
    return 0;
}

你可能感兴趣的:(hdu 3631 Shortest Path(Floyd))