296D codeforces - Greg and graph


296D codeforces - Grefg and graph







Solution in C++


#include <bits/stdc++.h>
using namespace std;

typedef long long int lli;
#define PB push_back
#define N      502

int n,mat[N][N],ord[N];
const lli M = (int)1e10;
lli dp[N][N],col[N],row[N];
lli sol;
bool ok[N];

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        for(int j = 0 ; j< n;j++)
            scanf("%I64d",&mat[i][j]),dp[i][j]=M;
    for(int i=0;i<n;i++)
        scanf("%d",&ord[i]),ord[i]--;
    vector<lli> sols(n,0);
    for(int k = n-1;k>=0;k--)
    {
        int nod = ord[k];
        ok[nod]=1;
        for(int q = 0;q<n;q++)
        {
            if(q==nod)continue;
            if(ok[nod])
            {
                dp[nod][q]=min(dp[nod][q],1LL*mat[nod][q]);
                dp[q][nod]=min(dp[q][nod],1LL*mat[q][nod]);
            }
        }
        for(int i =0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(i==j)continue;
                if(dp[i][nod]+dp[nod][j]<dp[i][j])
                    dp[i][j] =dp[i][nod]+dp[nod][j];
            }
        }
        for(int l = 0;l<n;l++)
            for(int o=0;o<n;o++)
                if(ok[l]&&ok[o])
                    sols[k]+=(dp[l][o]<M?dp[l][o]:0);
    }
    for(int i=0;i<n;i++)
        printf("%I64d ",sols[i]);
    return 0;
}
Share on Google Plus

About Mohamed Sobhi

My name is Mo.Sobhy ,I study at School excelling high school in Ain Shams ,Cairo
My hobbies is programming ,and web development ,playing chess and writing horror stories.
    Blogger Comment
    Facebook Comment

0 التعليقات:

إرسال تعليق