UVa 10681

another simple and easy dp
state:
    dp[i][j] - can be 0 or 1, whether can reach city j at day i
transition:
    dp[i][j]=1 if l[k][j]&&d[i-1][k], 1<=k<=n
    for all city k, if there is a link between k and j, and he was there yesterday
    then of course he can reach city j today
/* UVas */
/*****************************************************************************/
int C, L, S, E, D, t1, t2;
int l[101][101];
int d[201][101];
int main(void) {
    while (cin>>C>>L&&C>0&&L>=0) {
        memset(l, 0, sizeof(l));
        for (int i=0; i<L; i++) {
            cin>>t1>>t2;
            l[t1][t2]=1;
            l[t2][t1]=1;
        }
        cin>>S>>E>>D;
        memset(d, 0, sizeof(d));
        d[0][S]=1;
        for (int i=1; i<=D; i++) {
            for (int j=1; j<=C; j++) {
                for (int k=1; k<=C; k++) {
                    if (l[k][j]&&d[i-1][k]) {
                        d[i][j]=1;
                    }
                }
            }
        }
        d[D][E]?cout<<"Yes, Teobaldo can travel.":cout<<"No, Teobaldo can not travel.";
        cout<<endl;
    }
    return 0;
}
/*****************************************************************************/

No comments :

Post a Comment