【NOIp模拟赛】Tourist Attractions

Input file: tour.in
Output file: tour.out
Time limit: 1 seconds
Memory limit: 128 megabytes
在美丽的比特镇一共有 n 个景区,编号依次为 1 n,它们之间通过若干条双向道路连接。
Byteasar 慕名来到了比特镇旅游,不过由于昂贵的门票费,他只能负担起 4 个景区的门票费。他可
以在任意景区开始游览,然后结束在任意景区。
Byteasar 的旅游习惯比较特殊,一旦他路过了一个景区,他就一定会进去参观,并且他永远不会参
观同一个景区两次。所以他想知道,有多少种可行的旅游路线,使得他可以恰好参观 4 个景区呢?即,
有多少条简单路径恰好经过了 4 个点。
Input
第一行包含两个整数 n,表示景区的总数。
2 至第 n + 1 行,每行一个长度为 n 01 字符串,第 i + 1 行第 j 个字符为 0 表示 i j 之间
没有道路,为 1 表示有一条道路。
输入数据保证 (i; j) 的连接情况等于 (j; i) 的连接情况,且 (i; i) 恒为 0
Output
输出一行一个整数,即可行的路线总数。
Examples

tour.in tour.out
4
0101
1010
0101
1010
8


8 条路线分别为:
1->2->3->44->3->2->1
2->3->4->11->4->3->2
3->4->1->22->1->4->3
4->1->2->33->2->1->4
Page 4 of 7
Claris’ Contest # 2
Day 1
Notes

测试点编号 n
1 = 5
2 = 10
3 = 20
4 = 50
5 = 300
6 = 300
7 = 300
8 = 1500
9 = 1500
10 = 1500

 

分析

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
using namespace std;
typedef long long ll;
const int maxn=1500+5;
inline int read()
{
    int x=0,f=1; char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();}
    return x*f;
}
int n,du[maxn]; ll ans;
char a[maxn];
bitset<maxn>s[maxn];
int main()
{
    freopen("tour.in","r",stdin);
    freopen("tour.out","w",stdout);
    n=read();
    for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++)
    {
        char ch=getchar();
        while(ch!='0'&&ch!='1') ch=getchar();
        s[i][j]=ch-'0';
        if(s[i][j]) du[i]++;
    }
    for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++) if(i!=j&&s[i][j])
    {
        ans+=(du[i]-1)*(du[j]-1);
        ans-=(s[i]&s[j]).count();
    }
    printf("%lld\n",ans);
    return 0;
}

 

posted @ 2017-09-03 17:46  沐灵_hh  阅读(591)  评论(0编辑  收藏  举报