查看: 2373|回復: 0
打印 上一主題 下一主題

[UVa] 259 - Software Allocation

[複製鏈接]
  • TA的每日心情
    鬱悶
    2015-5-15 22:38
  • 簽到天數: 33 天

    [LV.5]常住居民I

    75

    主題

    302

    帖子

    766

    積分

    版主

    TFcis - 105 附設監工官

    Rank: 7Rank: 7Rank: 7

    積分
    766

    台南一中資訊社程式設計達人 - 2014

    跳轉到指定樓層
    樓主
    發表於 2014-5-3 22:38:24 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式

    趕快加入我們來參與討論吧!

    您需要 登錄 才可以下載或查看,沒有帳號?加入我們

    x
    本帖最後由 jd3 於 2014-5-4 16:30 編輯


    題目:有一些程式類型為A~Z,要再編號0~9的電腦上執行
    .          每種程式只能在特定幾台電腦上執行,請試著找出使所有程式能同時執行的安排
    .          同一台電腦無法同時執行多個程式


    注意事項:
               1.測資間有空行,最後一筆測資後接的是EOF
               2.不要遺漏驚嘆號


    解法:各種二分圖匹配



    AC Code :
    /*
            AC
            472~475 ms
    */

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<vector>

    using namespace std;


    vector<int> x[500],y[500];
    int xp[500], yp[500];
    int xcount, ycount;
    int xtype[500];


    int xvist[500], yvist[500];


    bool dfs(int node, int step)
    {
            bool found = false;
            if(step&1)        //Y side (com numbers)
            {
                    yvist[node] = true;
                    if(yp[node] == -1)
                            found = true;
                    else
                            found = dfs(yp[node], step+1);
                    yvist[node] = false;
            }
            else                //X side
            {
                    xvist[node] = true;
                    for(int i = 0 ; i < x[node].size() ; i++)
                    {
                            int next = x[node][i];
                            if((!yvist[next]) && xp[node] != next)
                                    if(dfs(next, step+1))
                                    {
                                            found = true;
                                            xp[node] = next;
                                            yp[next] = node;
                                            break;
                                    }
                    }
                    xvist[node] = false;
            }
            
            return found;
    }



    int main()
    {
            char str[500];
            bool going = true;
             
            while(going)
            {
                    //init
                    xcount = 0;
                    ycount = 0;
                    for(int i = 0 ; i < 10 ; i++)
                            y[i].clear();
                    for(int i = 0 ; i < 300 ; i++)
                            x[i].clear();
                    memset(xp,-1,sizeof(xp));
                    memset(yp,-1,sizeof(yp));
                    memset(xvist,0,sizeof(xvist));
                    memset(yvist,0,sizeof(yvist));
                   
                    //inputs
                    while(1)
                    {
                            if(!fgets(str,200,stdin))
                            {
                                    going = false;
                                    break;
                            }
                            if(str[0] == '\n')
                                    break;
                            
                            
                            int a = str[0];
                            for(int i = 0 ; i < str[1]-'0' ; i++)
                            {
                                    for(int i = 3 ; str[i] != ';' ; i++)
                                    {
                                            xtype[xcount] = a;
                                            x[xcount].push_back(str[i]-'0');
                                            y[str[i]-'0'].push_back(xcount);
                                    }
                                    xcount++;
                            }
                    }
                   
                   
                   
                    //process
                    bool update = true;
                    while(update)
                    {
                            update = false;
                            for(int i = 0 ; i < xcount ; i++)
                                    if(xp[i]==(-1) && dfs(i,0))
                                            update = true;
                    }
                   
                   
                    //ans
                    bool solved = true;
                    for(int i = 0 ; i < xcount ; i++)
                            if(xp[i] == -1)
                            {
                                    puts("!");
                                    solved = false;
                                    break;
                            }
                    if(!solved)
                            continue;
                    for(int i = 0 ; i < 10 ; i++)
                            if(yp[i] != -1)
                                    putchar(xtype[yp[i]]);
                            else
                                    putchar('_');
                   
                    putchar('\n');
                   
            }
            
            return 0;
    }
    程式說明:x,y儲存可連接的對面的結點編號 (自0)
    xp存目前匹配對象
    xtype記錄x邊的結點是哪種程式(大寫英文字母)
    dfs尋找增廣路徑,此以使x邊全部匹配為目標
    (我的yvist好像沒用到)

    備註:可以先判斷程式數量是否超過電腦數量

    評分

    參與人數 1金幣 +3 收起 理由
    Sylveon + 3

    查看全部評分

    <這是個人簽名欄位>
    回復

    使用道具 檢舉

    您需要登錄後才可以回帖 登入 | 加入我們

    本版積分規則

    快速回覆 返回頂部 返回列表