bzoj5056: OI游戏

来源:互联网 发布:东莞制造业知乎 编辑:程序博客网 时间:2024/06/07 15:45

我们van♂游戏233。

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;queue<int> q;int read(){    char ch=getchar();int f=0;    while(ch<'0'||ch>'9')ch=getchar();    while(ch>='0'&&ch<='9'){f=(f<<1)+(f<<3)+ch-'0';ch=getchar();}    return f;}struct node{    int from;    int to;    int next;    int w;}edge[50005];int tot1=0,head[1005],n,m,dis[1005];bool vis[1005];char s[5005];const int mod=1000000007;void add(int u,int v,int w){    edge[tot1].from=u;    edge[tot1].to=v;    edge[tot1].w=w;    edge[tot1].next=head[u];    head[u]=tot1++;}void spfa(){    q.push(1);    dis[1]=0;    while(!q.empty())    {        int x=q.front();        q.pop();vis[x]=0;        for(int i=head[x];i!=-1;i=edge[i].next)        {            if(dis[edge[i].to]>dis[x]+edge[i].w)            {                dis[edge[i].to]=dis[x]+edge[i].w;                if(!vis[edge[i].to])                {                    q.push(edge[i].to);                    vis[edge[i].to]=1;                }            }        }    }}void solve(){    long long ans=1;    for(int i=2;i<=n;i++)    {        long long tot=0;        for(int j=head[i];j!=-1;j=edge[j].next)        {            if((long long)dis[i]==(long long)dis[edge[j].to]+(long long)edge[j].w) tot++;        }        //cout<<tot<<" ";        ans=ans*tot%mod;    }    cout<<ans;}int main(){    memset(head,-1,sizeof(head));    memset(dis,0x7f7f7f7f,sizeof(dis));    n=read();    for(int i=1;i<=n;i++)    {        scanf("%s",s+1);        for(int j=1;j<=n;j++)        {            if(s[j]!='0')            {                add(i,j,s[j]-'0');            }        }    }    spfa();    //for(int i=1;i<=n;i++)    //cout<<dis[i]<<" ";    solve();}
原创粉丝点击