The 2018 ACM-ICPC China JiangSu Provincial Programming Contest K Road

JSZKC is the king of his kingdom.

His kingdom has NN cities, numbered from 0 to N-1. And the cities are connected by some roads which means you can travel from one city to any other city by roads. Each roads have its length.

However, JSZKC wants to delete some roads(may be 0) and let the kingdom keep the following requirements:

  1. All the cities are still connected.
  2. The number of the rest roads is minimal
  3. For all the cities, its shortest distance to 0 doesn't change.

JSZKC wants to know how many ways to delete roads with these requirements. Two ways are different if one road is deleted in one way while not in the other way.

As the result may be very large, please output the result mod 1000000007.

Input Format

The input file contains several test cases, each of them as described below.

  • The first line of the input contains one integers N(1≤N≤100), giving the cities of the kingdom.
  • The next follows N lines with each line N integers. Let's define the j^{th}jth integer in the i^{th}ith line as f[i][j]. Then it's guaranteed that f[i][j]=f[j][i]and 0≤f[i][j]≤9. If f[i][j]>0, then there is a road with length f[i][j]connecting city i and city j. If f[i][j]=0, then there is no road between city i and city j.

There are no more than 100 test cases.

Output Format

One line per case, an integer indicates the answer.

樣例輸入

2
01
10
4
0123
1012
2101
3210

樣例輸出

1
6

題鏈接

題意:這個王國有n個城市,編號爲0到n-1,它們之間是可以相互到達的,每條邊的大小範圍1~9  ,   0表示沒有邊

這個人想刪除一些邊,滿足(1)n個城市還可以相互到達。(2)剩下來的邊的數量最小 (3) 對於除了編號爲0以外的城市,到達0的最小距離始終不變。然後問你有多少種刪除邊的方案。對1e9+7取餘。

 

解題思路:通過分析這三個條件,很容易知道是找一個最小生成樹方案數,而且邊最少。那麼我們可以先求出來 編號爲1~n-1的城市到編號爲0的城市的最小距離,然後統計一下從0到u有多少條道路是滿足最小距離的。那麼最終答案就是1到b-1 道路數量的乘積 對1e9+7取餘。 由於城市的數量最大100,那麼我們就可以用floyd算法進行操作。get到了新的知識。 

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=110;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
#define ll long long
int n,m,map[maxn][maxn],f[maxn][maxn];
ll fa[maxn];
char ch[maxn][maxn];
int main(){
	int i,j,k;
	while(scanf("%d",&n)!=EOF){
		for(i=1;i<=n;i++){
			scanf("%s",ch[i]+1);
		}
		for(i=1;i<=n;i++){
			for(j=1;j<=n;j++){
				map[i][j]=ch[i][j]-'0';
				if(!map[i][j]) map[i][j]=inf;
				f[i][j]=map[i][j];
			}
		}
		map[1][1]=0;
		memset(fa,0,sizeof(fa));
		for(k=1;k<=n;k++){
			for(i=1;i<=n;i++){
				for(j=1;j<=n;j++){
					if(i!=j&&i!=k&&j!=k){
						map[i][j]=min(map[i][j],map[i][k]+map[k][j]);
					}
				}
			}
		}
		for(i=1;i<=n;i++){
			for(j=1;j<=n;j++){
				if(i!=j){
					if(map[1][j]==map[1][i]+f[i][j]){
						fa[j]++;
					}
				}
			}
		}
		ll ans=1;
		for(i=2;i<=n;i++) ans*=fa[i],ans%=mod;
		printf("%lld\n",ans);
	}
	return 0;
}

 

發佈了79 篇原創文章 · 獲贊 12 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章