poj 1161 Walls

//poj 1161 Walls 平面图的对偶图
/*
题解: 求平面图的对偶图,然后按题意枚举见面地点就好了。
PS: 这题WA了N次,有点绝望,错在把n个点也加入图,分别
与它所属的几个面相连,这样就会造成原本不相连的面可能会
通过某个点相连。
*/
#include <iostream>
#include <vector>
using namespace std;
const int inf = 1<<28;

int n,m,l;
int p[300];

int pos[31];

int con[210][210],hash[260][260];
int Q[210],mark[210],wt[210];
vector<int> tag[31];
int bfs(int s)
{
    memset(mark,0,sizeof(mark));
    int head=0,tail=0;
    Q[tail++]=s;
    mark[s]=1;
    wt[s]=0;
    while (head<tail)
    {
          int k=Q[head++];
          for (int i=1;i<=m;i++)
          if (!mark[i] && con[i][k])
          {
                       wt[i]=wt[k]+1;
                       mark[i]=1;
                       Q[tail++]=i;
          }
    }
    int sum=0;
    for (int i=1;i<=l;i++)
    {
        int ans=inf;
        for (int j=0;j<tag[i].size();j++)
        {
            ans=min(ans,wt[tag[i][j]]);
        }
        sum+=ans;
    }
    return sum;
}
int main()
{
    while (scanf("%d%d%d",&m,&n,&l)!=EOF)
    {
          int a,k;
          memset(hash,0,sizeof(hash));
          memset(con,0,sizeof(con));
          for (int i=1;i<=l;i++) { tag[i].clear(); scanf("%d",&pos[i]);}
          for (int i=1;i<=m;i++)
          {
              scanf("%d",&k);
              for (int j=0;j<k;++j)
              {
                  scanf("%d",&p[j]);
                  for (int v=1;v<=l;v++)
                  if (pos[v]==p[j])
                  {
                            tag[v].push_back(i);
                            break;      
                  }
                  if (j){
                         if(a=hash[p[j]][p[j-1]])
                         con[a][i]=con[i][a]=1;
                         else hash[p[j-1]][p[j]]=i;
                  }
              }
              if (a=hash[p[0]][p[k-1]])
                    con[a][i]=con[i][a]=1;
              else hash[p[k-1]][p[0]]=i;
          }
         
          int ans=inf;
          for (int i=1;i<=m;i++)
          {
              ans=min(ans,bfs(i));
          }
          printf("%d/n",ans);
         
         
         
    }
    system("pause");
    return 0;
   
}

你可能感兴趣的:(poj 1161 Walls)