HDU 6026 Deleting Edges

Title: http://acm.hdu.edu.cn/showproblem.php?pid=6026

Question meaning: Given a graph, it is required to delete some edges, and then make the deleted graph a tree, and the distance from each point to 0 is the shortest distance in the original graph.

Solution: Dijstra calculates the distance from each point to the origin, then enumerates each point, calculates the distance from its adjacent point to him as the number of the shortest distances to itself, and multiplies them.

AC:

#include <iostream>
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;

typedef long long ll;
const int MOD=1e9+7;
ll n, dist[110], vis[110], a[110][110];
char b[110][110];

void dijkstra()
{
    for(int i = 0; i < n; i++){
        dist[i] = INF;
    }
    dist[0]=0;
    for(int i = 0; i < n; i++){
        ll mini = INF, k=-1;
        for(int j = 0; j < n; j++){
            if(!vis[j]&&dist[j]<mini){
                mini = dist[j];
                k = j;
            }
        }
        vis[k] = 1;
        for(int j = 0; j < n; j++){
            if(!vis[j]&&a[k][j]&&dist[j] > dist[k]+a[k][j]){
                dist[j] = dist[k]+a[k][j];
            }
        }
    }
}

intmain ()
{
    while(cin >> n)
    {
        memset(vis,0,sizeof(vis));
        for(int i = 0; i < n; i++){
            cin >> b[i];
            for(int j = 0; j < n; j++){
                a[i][j] = b[i][j]-'0';
            }
        }
        dijkstra();
         ll tmp, ans=1;
        for(int i = 1; i < n; i++){
            tmp = 0;
            for(int j = 0; j < n; j++){
                if(a[j][i]&&dist[i] == dist[j]+a[j][i]){
                    tmp++;
                }
            }
            years = (years*tmp)% MOD;
        }
        cout << ans << endl;

    }
    return 0;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325027945&siteId=291194637