Cod sursa(job #1205676)

Utilizator gabib97Gabriel Boroghina gabib97 Data 7 iulie 2014 17:31:41
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.44 kb
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <queue>
#include <ctype.h>
#define pp pair<int,int>
#define inf 100000000
using namespace std;
int n,m,s,i,*d,j,C[101][101];
char *b;
vector<pp> G[5005];
class comp
{
public:
 bool operator() (const int& a, const int& b)
  {
    return (d[a]>d[b]);
  }
};
priority_queue<int, vector<int> ,comp> Q;
void init()
{
    scanf("%i",&n);
    for (i=1;i<=n;i++)
        for(j=1;j<=n;j++)
    {
        scanf("%i",&C[i][j]);
        if (C[i][j]!=0) G[i].push_back(pp(j,C[i][j]));
    }
    d=(int*) calloc(n+2,sizeof(int));
}
void dijkstra(int s)
{
    int i,x,y,z,c;
    for (i=1;i<=n;i++) d[i]=inf;
    d[s]=0; Q.push(s);
    while(!Q.empty())
    {
        x = Q.top();
        Q.pop();
        z = G[x].size();
        for(i=0;i<z;i++)
        {
            y = G[x][i].first;
            c = G[x][i].second;
            if(d[y] > d[x] + c)
            {
                d[y] = d[x] + c;
                Q.push(y);
            }
        }
    }
}
int main()
{
    freopen ("royfloyd.in","r",stdin);
    freopen ("royfloyd.out","w",stdout);
    init();
    for (j=1;j<=n;j++)
    {
        dijkstra(j);
        for(i=1;i<=n;i++)
        {
            if (d[i]!=inf) printf("%i",d[i]);
            else printf("0");
            if (i<n) printf(" ");
        }
        printf("\n");
    }

    fclose(stdin);
    fclose(stdout);
    return 0;
}