栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

zoj 2031 Song List

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

zoj 2031 Song List

#include<iostream>#include<set>#include<string.h>#include<stdlib.h>#include<algorithm>#include<stdio.h>using namespace std;enum {    SIZ = 10004,};struct Node {    int pos;    char author[12];    char type;};int num;Node tree[SIZ];int ptr[SIZ];unsigned step[SIZ];struct author_cmp {    bool operator ()(const int&a, const int&b)const{        return strcmp(tree[a].author, tree[b].author) < 0;    }};struct set_cmp {    bool operator ()(const int&a, const int&b)const{        if (step[a] != step[b]) return step[a] < step[b];        return a < b;    }}scmp;set<int, set_cmp> tab;int src, dst;void update(int p, int s, char t){    unsigned v = step[s] + 1;    v += (tree[s].type!=t);    if (step[p] == -1 || step[p] > v){        if (step[p] != -1) tab.erase(p);        step[p] = v;        tab.insert(p);        tree[p].type = t;    }}unsigned fun(){    tab.clear();    step[src] = 0;    tab.insert(src);    int nex;    while(!tab.empty()){        src = *tab.begin(); tab.erase(tab.begin());        if (src == dst){ return step[dst];        }        nex = (src+1)%num;        update(nex, src, 0);        nex = (src-1+num)%num;        update(nex, src, 0);        nex = ptr[(tree[src].pos+1)%num];        update(nex, src, 1);        nex = ptr[(num+tree[src].pos-1)%num];        update(nex, src, 1);    }    return -1;}int readIn(){    if (scanf("%d%d%d", &num,&src,&dst) < 0)        return 0;    --src, --dst;    memset(step, -1, sizeof(step));    for (int i=0; i<num; ++i){        ptr[i] = i;        tree[i].type = 0;        scanf("%s", tree[i].author);         scanf("%s", tree[i].author);    }    sort(ptr, ptr+num, author_cmp());    for (int i=0; i<num; ++i){        tree[ptr[i]].pos = i;    }    return 1;}int main(){    while(readIn() > 0){        unsigned v = fun();        printf("%un", v);    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/378482.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号