首页 > 代码库 > usaco-2.4-comhome-pass

usaco-2.4-comhome-pass

恶补了好几天的算法,英文版看得更懂,呵呵:

/*ID: qq104801LANG: C++TASK: comehome*/#include <iostream>#include <fstream>#include <cstring>#include <vector>#include <list>#include <set>#include <queue>#include <cstdio>#include <algorithm>#include <cmath>#define NMAX 52#define INF 99999using namespace std;int p;int gg[NMAX][NMAX];int dd[NMAX];int vv[NMAX];int result;int change(char cc){    int index=-1;    if(cc>=A && cc<=Z)        index=cc-A;    else if(cc>=a && cc<=z)        index=cc-a+26;    else index=-1;    return index;}int dijkstra(int s){    int min,k;    for(int i=0;i<NMAX;i++)    {        vv[i]=false;        dd[i]=gg[s][i];    }    dd[s]=0;    vv[s]=1;    for(int i=0;i<NMAX;i++)    {        min=INF;        for(int j=0;j<NMAX;j++)            if(!vv[j] && dd[j]<min)            {                min=dd[j];                k=j;            }        vv[k]=1;        for(int j=0;j<NMAX;j++)            if(!vv[j] && (min+gg[k][j]<dd[j]))            {                dd[j]=min+gg[k][j];                        }    }    int mmin=INF;    for(int i=0;i<NMAX;i++)    {        if(dd[i]<mmin && i>=0 && i<=25 && dd[i]>0)        {            mmin=dd[i];            result=i;        }    }    return mmin;}void test(){        freopen("comehome.in","r",stdin);    freopen("comehome.out","w",stdout);        char ca,cb;    int d;    for(int i=0;i<NMAX;i++)    {        gg[i][i]=INF;        for(int j=0;j<NMAX;j++)            gg[i][j]=INF;    }    memset(vv,0,sizeof(vv));       cin>>p;    while(p--)    {        cin>>ca>>cb>>d;        int ia=change(ca);        int ib=change(cb);        if(d<gg[ia][ib])        {            gg[ia][ib]=d;            gg[ib][ia]=d;        }    }    int ans=dijkstra(25);    char first=A+result;    cout<<first<<" "<<ans<<endl;}int main () {            test();            return 0;}

test data:

USER: cn tom [qq104801]TASK: comehomeLANG: C++Compiling...Compile: OKExecuting...   Test 1: TEST OK [0.008 secs, 3384 KB]   Test 2: TEST OK [0.005 secs, 3384 KB]   Test 3: TEST OK [0.003 secs, 3384 KB]   Test 4: TEST OK [0.008 secs, 3384 KB]   Test 5: TEST OK [0.008 secs, 3384 KB]   Test 6: TEST OK [0.014 secs, 3384 KB]   Test 7: TEST OK [0.032 secs, 3384 KB]   Test 8: TEST OK [0.008 secs, 3384 KB]   Test 9: TEST OK [0.005 secs, 3384 KB]All tests OK.Your program (‘comehome‘) produced all correct answers! This is your submission #3 for this problem. Congratulations!Here are the test data inputs:------- test 1 ----4d Z 10b d 2A b 4C d 5------- test 2 ----25m Z 1000A m 1000B m 999C m 998D m 997E m 996F m 995G m 994H m 993I m 992J m 991K m 990L m 989M m 988N m 987O m 986P m 985Q m 984R m 983S m 982T m 981U m 980V m 979W m 978X m 977------- test 3 ----28Z a 1000a b 1000b c 1000c d 1000d e 1000e f 1000f g 1000g h 1000h i 1000i j 1000j k 1000k l 1000l m 1000m n 1000n o 1000o p 1000p q 1000q r 1000r s 1000s t 1000t u 1000u v 1000v w 1000w x 1000x y 1000y z 1000z A 1000A B 1000------- test 4 ----51A a 836B b 836C c 836D d 836E e 836F f 836G g 836H h 836I i 836J j 836K k 836................

 

 

usaco-2.4-comhome-pass