POJ 1778 All Discs Considered(拓扑排序)

点我看题目

题意 :其实题意我也说不清楚,因为比赛的时候我盯着看了1个小时也没看懂。。。。就是两个磁盘,第一个有n1的安装包,编号为1~n1,第二个有n2个安装包,编号为n1~n2。给你d对关系,(x,y)代表着安装x之前要先安装y。然后让你往里插入这两个磁盘,因为一次只能插一次,所以要满足他给的条件的时候要频繁的来回换。还有,要注意的一点是,无论先往里边插第一个还是第2个,第一次插的时候算一次,最后一次拔的时候算一次。

思路 :其实我真不知道这是拓扑排序,,,,,后来才知道的。。。。。

//#include <iostream>
//#include <string.h>
//#include <stdio.h>
//#include <algorithm>
//
//const int maxn = 424567 ;
//char sh[maxn] ;
//char ch[maxn] ;
//
//using namespace std;
//
//int main()
//{
//    while(gets(sh))
//    {
//        int x = 0,j ;
//        int len = strlen(sh) ;
//        //   printf("%d",len);
//        for(int i = 0 ; i < len ; )
//        {
//            j = i + 1;
//            if(sh[i] == sh[j])
//            {
//                while(sh[i] == sh[j])
//                {
//                    j++ ;
//                    if(j - i >= 9)
//                        break ;
//                }
//                ch[x++] = j-i+'0' ;
//                ch[x++] = sh[i] ;
//                i += (j-i) ;
//            }
//            else
//            {
//                while((sh[j] != sh[j+1] && j+1 < len) || j == len-1)
//                    j++ ;
//                ch[x++] = '1' ;
//                for(int ii = i ; ii < j ; ii++)
//                {
//                    ch[x++] = sh[ii] ;
//                    if(sh[ii] == '1' )
//                        ch[x++] = '1' ;
//                    //if(j == 0 && sh[j] != sh[j+1])
//                    //ch[x++] = '1' ;
//                }
//                ch[x++] = '1' ;
//                i += (j-i) ;
//            }
//        }
//        ch[x] = '' ;
//        printf("%s
",ch) ;
//    }
//    return 0;
//}
//
//#include <stdio.h>
//#include <string.h>
//#include <iostream>
//
//using namespace std ;
//int  work(int m,int n)
//{
//    int sum = 1;
//    for(int i = 1 ; i <= n ; i++)
//    sum *= 10 ;
//    return m*sum ;
//}
//int main()
//{
//     char ch[5] ;
//     while(scanf("%s",ch) != EOF)
//     {
//         if(strcmp(ch,"00e0") == 0) break ;
//         int s = (ch[0]-'0')*10+ch[1]-'0' ;
//         int x = ch[3]-'0' ;
//
//         int sum = work(s,x) ;
//         int i = 1,num = 0  ;
//         while(i <= sum)
//         {
//              i *= 2 ;
//              num++ ;
//         }
//         printf("%d
",2*(sum - i/2)+1) ;
//     }
//     return 0 ;
//}
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <queue>

using namespace std ;

const int maxn = 112166 ;
const int INF = 999999999 ;

int n,n1,n2,d;
int head[maxn],cnt,ans,deg[maxn],degr[maxn] ;


struct node
{
    int u,v,w ;
    int next ;
} Edge[maxn] ;
queue<int>Q[2] ;

void addedge(int u,int v)
{
    Edge[cnt].u = u ;
    Edge[cnt].v = v ;
    Edge[cnt].next = head[u] ;
    head[u] = cnt++ ;
}

void toposort(int u)
{
    int sum = 0 ;
    for(int i = 1 ; i <= n ; i++)
    {
        if(deg[i] == 0)
        {
            if(i <= n1) Q[0].push(i) ;
            else Q[1].push(i) ;
        }
    }
    int v,w ;
    while(!Q[u].empty() || !Q[u^1].empty())
    {
        while(!Q[u].empty())
        {
            v = Q[u].front() ;
            Q[u].pop() ;
            for(int i = head[v] ; i + 1 ; i = Edge[i].next)
            {
                w = Edge[i].v ;
                deg[w]-- ;
                if(!deg[w])
                {
                    if(w <= n1) Q[0].push(w) ;
                    else Q[1].push(w) ;
                }
            }
        }
        u = u^1 ;
        sum++ ;
    }
    if(sum < ans) ans = sum ;
}

void Init()
{
    n = n1+n2 ;
    memset(head,-1,sizeof(head)) ;
    memset(degr,0,sizeof(degr)) ;
    cnt = 0 ;
    ans = INF ;
}
int main()
{
    while(~scanf("%d %d %d",&n1,&n2,&d))
    {
        if(n1 == 0 && n2 == 0 && d == 0) break ;
        Init() ;
        int x,y ;
        for(int i = 1 ; i <= d ; i++)
        {
            scanf("%d %d",&x,&y) ;
            degr[x]++ ;
            addedge(y,x) ;
        }
        for(int i = 1 ; i <= n ; i++)
        deg[i] = degr[i] ;
        toposort(0) ;
        for(int i = 1 ; i <= n ; i++)
        deg[i] = degr[i] ;
        toposort(1) ;
        printf("%d
",ans+1) ;
    }
    return 0 ;
}
View Code
原文地址:https://www.cnblogs.com/luyingfeng/p/3561110.html