usaco Canada Tour

一开始以为是搜索,发现有从西向东走的要求后,去看别人博客才知道是DP,真的怀疑自己得改行去搬砖。

/*
ID: modengd1
PROG: tour
LANG: C++
*/
#include <iostream>
#include <string.h>
#include <cstring>
#include <vector>
#include <stdio.h>
using namespace std;
vector<string> Names;
int N,M;
bool G[100][100];
int getindex(string na)
{
    for(int i=0;i<Names.size();i++)
    {
        if(na==Names[i])
            return i;
    }
    Names.push_back(na);
    return Names.size()-1;
}
void getinput()
{
    memset(G,false,sizeof(G));
    scanf("%d%d",&N,&M);
    getchar();
    for(int i=0;i<N;i++)//输入城市名
    {
        string name;
        char ch;
        while(scanf("%c",&ch)&&ch!='\n')
            name.push_back(ch);
        getindex(name);
    }
    for(int i=0;i<M;i++)//输入航班路线
    {
        int a,b;
        string name;
        char ch;
        while(scanf("%c",&ch)&&ch!=' ')
            name.push_back(ch);
        a=getindex(name);

        name.clear();
        while(scanf("%c",&ch)&&ch!='\n')
            name.push_back(ch);
        b=getindex(name);
        G[a][b]=G[b][a]=true;
    }

}

int main()
{
    freopen("tour.in","r",stdin);
    freopen("tour.out","w",stdout);
    getinput();
    int dp[101][101];
    dp[0][0]=1;
    for(int i=0;i<N;i++)
    {
        for(int j=i+1;j<N;j++)
        {
            dp[i][j]=-1;
            for(int k=0;k<j;k++)
            {
                if(G[k][j]&&dp[i][k]>0&&dp[i][k]>dp[i][j])
                    dp[i][j]=dp[i][k];
            }
            dp[j][i]=++dp[i][j];
        }
    }
    int ans=1;
    for(int i=0;i<N;i++)
        if(G[i][N-1])
            ans=max(ans,dp[i][N-1]);
    cout<<ans<<endl;
    return 0;
}

  

posted on 2015-10-08 22:29  insaneman  阅读(166)  评论(0编辑  收藏  举报

导航