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万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章