HDU 2254 Olympic Games (matrix), hdu2254 Olympic Matrix

Source: Internet
Author: User

HDU 2254 Olympic Games (matrix), hdu2254 Olympic Matrix

Address: HDU 2254

You have to speak out .. The data for this question is weak and pitfall .. The sample can only be AC .. And .. Actually, there is a duplicate edge .. WA for one night ..

Let's get down to the truth ..

According to the properties of the reachable matrix in discrete mathematics, we know that the sum of the First n power of the adjacent matrix of a directed graph is the reachable matrix, then the number of paths within the [t1-t2] is required, this is because t1 = 0 is 0. Assuming that the adjacent matrix is A, the requirement is A ^ (t1-1) + A ^ (t1) +... + A ^ (t2-1), why is it starting from the t1-1, because the adjacent matrix itself represents the result of taking A step.

Then we can add discretization.

The Code is as follows:

#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <stdlib.h>#include <math.h>#include <ctype.h>#include <queue>#include <map>#include <set>#include <algorithm>using namespace std;#define LL __int64const int mod=2008;int n;LL q[40];struct matrix{    int ma[32][32];} mat[10002];matrix Mult(matrix x, matrix y){    int i, j, k;    matrix tmp;    memset(tmp.ma,0,sizeof(tmp.ma));    for(i=0; i<n; i++)    {        for(k=0; k<n; k++)        {            for(j=0; j<n; j++)            {                tmp.ma[i][j]=(tmp.ma[i][j]+x.ma[i][k]*y.ma[k][j])%mod;            }        }    }    return tmp;}int find1(LL x){    int i;    for(i=0; i<n; i++)    {        if(q[i]==x)        {            return i;        }    }    return -1;}void Pow(){    for(int i=1; i<=10001; i++)    {        mat[i]=Mult(mat[i-1],mat[0]);    }}int main(){    int m, k, i, j, t1 ,t2, u, v;    LL v1, v2;    while(scanf("%d",&m)!=EOF)    {        memset(mat[0].ma,0,sizeof(mat[0].ma));        n=0;        while(m--)        {            scanf("%d%d",&u,&v);            int f1=find1(u);            if(f1<0)            {                q[n++]=u;                f1=n-1;            }            int f2=find1(v);            if(f2<0)            {                q[n++]=v;                f2=n-1;            }            mat[0].ma[f1][f2]++;        }        scanf("%d",&k);        Pow();        while(k--)        {            scanf("%I64d%I64d%d%d",&v1,&v2,&t1,&t2);            int f1=find1(v1);            int f2=find1(v2);            if(f1<0||f2<0)            {                puts("0");                continue ;            }            int ans=0;            for(i=t1-1; i<t2; i++)            {                if(i==-1) continue ;                ans+=mat[i].ma[f1][f2]%mod;                ans%=mod;            }            printf("%d\n",ans);        }    }    return 0;}



Why am I wrong when I asked Daniel hdu 1575Tr A's binary matrix?

Examples/l. c, nmk {jhxfjdghjkhjkhgd} zghjsbfhz [jshdjgnsjhjg} jhgsdfbhvhbhkdfk (jhsdjkfgb) jgfusbjkbfbniamj
 
A matrix problem of ACM

I am using the AC code, but I am not using the AC on the ultraviolet.
The editor of the UVA is a little different from that of the ACM website in China, and the CERNET is not accessible, so it is not submitted on the UVA. This topic also exists in POJ.

The algorithm for this question is dynamic planning.
# Include <stdio. h>
# Include <memory. h>
Int n, a [100] [100], B [100];
Int dp1 ()
{
Int sum = 0, c = 0, max = B [0];
For (int I = 0; I <n; ++ I)
{
If (max <B [I]) max = B [I];
If (c> 0) c + = B [I];
Else c = B [I];
If (c> sum) sum = c;
}
If (max <0) return max;
Else return sum;
}

Int dp ()
{
Int sum =-1000000000;
For (int I = 0; I <n; ++ I)
{
Memset (B, 0, sizeof (B ));
For (int j = I; j <n; ++ j)
{
For (int k = 0; k <n; ++ k)
B [k] + = a [j] [k];
Int t = dp1 ();
If (sum <t) sum = t;
}
}
Return sum;
}
Int main ()
{
While (scanf ("% d", & n )! =-1)
{
For (int I = 0; I <n; ++ I)
For (int j = 0; j <n; ++ j)
Scanf ("% d", & a [I] [j]);
Printf ("% d \ n", dp ());
}
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.