ZOJ 3644 Kitty's Game (Guang search, map, vector in STL)

Source: Internet
Author: User
Kitty's Game
Time Limit: 2 Seconds Memory Limit: 65536 KB

Kitty is a little cat. She is crazy about a game recently.

There areNScenes in the game (mark from 1N). Each scene has a numberPi. Kitty's score will become least_common_multiple (X,Pi) When Kitty enterITh scene.XIs the score that Kitty had previous. Notice that Kitty will become mad If she go to another scene but the score didn't change.

Kitty is staying in the first scene now (P1 score). Please find out how many paths which can arrive atNTh scene and hasKScores at there. Of course, you can't make Kitty mad.

We regard two paths different if and only if the edge sequence is different.

Input

There are multiple test cases. For each test case:

The first line contains three integerN(2 ≤N≤ 2000 ),M(2 ≤M≤ 20000 ),K(2 ≤K≤ 106). Then followedMLines. Each line contains two integerU,V(1 ≤U,V≤N, u =v) indicate we can goVTh scene fromUTh scene directly. The last line of each case contains n integerPI (1 ≤P(I ≤ 106 ).

Process to the end of input.

Output

One line for each case. The number of paths module 1000000007.

Sample Input
5 6 841 22 51 33 51 44 51 5 4 12 21
Sample Output
2


I did not think of a method during the competition.
After the game, I suddenly realized the problem.
Map can be easily used.
You can search directly.
Specific Code:
#include<stdio.h>#include<vector>#include<iostream>#include<map>#include<string.h>#include<queue>#include<algorithm>using namespace std;const int MAXN=2010;const int MOD=1000000007;vector<int>g[MAXN];map<int,int>dp[MAXN];int n,k;int score[MAXN];bool vis[MAXN];int gcd(int a,int b){    if(b==0)return a;    return gcd(b,a%b);}int bfs(){    int ret=0;    queue<int>q;    dp[1][score[1]]=1;    memset(vis,false,sizeof(vis));    while(!q.empty())q.pop();    q.push(1);    vis[1]=true;    while(!q.empty())    {        int cur=q.front();        q.pop();        vis[cur]=false;        if(cur==n)ret=(ret+dp[cur][k])%MOD;        for(int i=0;i<g[cur].size();i++)        {            int v=g[cur][i];            map<int,int>::iterator it;            for(it=dp[cur].begin();it!=dp[cur].end();it++)            {                int temp=it->first;                int lcm=temp/gcd(temp,score[v])*score[v];                if(lcm!=temp &&  k%lcm==0)                {                    dp[v][lcm]=(dp[v][lcm]+it->second)%MOD;                    if(!vis[v])                    {                        vis[v]=true;                        q.push(v);                    }                }            }        }        dp[cur].clear();    }    return ret;}int main(){    int m,u,v;    while(scanf("%d%d%d",&n,&m,&k)!=EOF)    {        for(int i=1;i<=n;i++)g[i].clear();        for(int i=1;i<=n;i++)dp[i].clear();        while(m--)        {            scanf("%d%d",&u,&v);            g[u].push_back(v);        }        for(int i=1;i<=n;i++)scanf("%d",&score[i]);        printf("%d\n",bfs());    }    return 0;}
 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.