zoukankan      html  css  js  c++  java
  • Spoj-NETADMIN Smart Network Administrator

    The citizens of a small village are tired of being the only inhabitants around without a connection to the Internet. After nominating the future network administrator, his house was connected to the global network. All users that want to have access to the Internet must be connected directly to the admin's house by a single cable (every cable may run underground along streets only, from the admin's house to the user's house). Since the newly appointed administrator wants to have everything under control, he demands that cables of different colors should be used. Moreover, to make troubleshooting easier, he requires that no two cables of the same color go along one stretch of street.

    Your goal is to find the minimum number of cable colors that must be used in order to connect every willing person to the Internet.

    Input

    t [the number of test cases, t<=500]
    n m k [n <=500 the number of houses (the index of the admin's house is 1)]
    [m the number of streets, k the number of houses to connect]
    h1 h2 ... hk [a list of k houses wanting to be conected to the network, 2<=hi<=n]
    [The next m lines contain pairs of house numbers describing street ends]
    e11 e12
    e21 e22
    ...
    em1 em2
    [next cases]

    Output

    For each test case print the minimal number of cable colors necessary to make all the required connections.

    Example

    Input:
    2
    5 5 4
    2 3 4 5
    1 2
    1 3
    2 3
    2 4
    3 5
    8 8 3
    4 5 7
    1 2
    1 8
    8 7
    1 3
    3 6
    3 2
    2 4
    2 5
    
    Output:
    2
    1
    

    Illustration to the first example

    Warning: large Input/Output data, be careful with certain languages 

    二分路径上出现过的最多种的颜色,然后网络流跑一跑看看能不能到达所有点

      1 #include<cstdio>
      2 #include<iostream>
      3 #include<cstring>
      4 #include<cstdlib>
      5 #include<algorithm>
      6 #include<cmath>
      7 #include<queue>
      8 #include<deque>
      9 #include<set>
     10 #include<map>
     11 #include<ctime>
     12 #define LL long long
     13 #define inf 0x7ffffff
     14 #define pa pair<int,int>
     15 #define mkp(a,b) make_pair(a,b)
     16 #define pi 3.1415926535897932384626433832795028841971
     17 using namespace std;
     18 inline LL read()
     19 {
     20     LL x=0,f=1;char ch=getchar();
     21     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
     22     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
     23     return x*f;
     24 }
     25 int n,m,k,cnt,S,T;
     26 int c[100010];
     27 struct edge{
     28     int to,next,v;
     29 }e[400010];
     30 struct ed{int x,y;}d[200010];
     31 int head[100010];
     32 int cur[100010];
     33 inline void ins(int u,int v,int w)
     34 {
     35     e[++cnt].to=v;
     36     e[cnt].next=head[u];
     37     head[u]=cnt;
     38     e[cnt].v=w;
     39 }
     40 inline void insert(int u,int v,int w)
     41 {
     42     ins(u,v,w);
     43     ins(v,u,0);
     44 }
     45 int h[100010];
     46 int q[1000010];
     47 int ans;
     48 inline bool bfs()
     49 {
     50     for (int i=0;i<=T;i++)h[i]=-1;
     51     int t=0,w=1;
     52     q[0]=S;h[S]=0;
     53     while (t!=w)
     54     {
     55         int now=q[t++];
     56         for(int i=head[now];i;i=e[i].next)
     57             if (e[i].v&&h[e[i].to]==-1)
     58             {
     59                 h[e[i].to]=h[now]+1;
     60                 q[w++]=e[i].to;
     61             }
     62     }
     63     return h[T]!=-1;
     64 }
     65 inline int dfs(int x,int f)
     66 {
     67     if (x==T||!f)return f;
     68     int w,used=0;
     69     for (int i=head[x];i;i=e[i].next)
     70         if (e[i].v&&h[e[i].to]==h[x]+1)
     71         {
     72             w=dfs(e[i].to,min(e[i].v,f-used));
     73             e[i].v-=w;
     74             e[i^1].v+=w;
     75             used+=w;
     76             if (f==used)return f;
     77         }
     78     if (!used)h[x]=-1;
     79     return used;
     80 }
     81 inline void rebuild(int mid)
     82 {
     83     for (int i=0;i<=T;i++)head[i]=0;
     84     S=1;T=n+1;cnt=1;
     85     for (int i=1;i<=k;i++)insert(c[i],T,1);
     86     for (int i=1;i<=m;i++)
     87     {
     88         insert(d[i].x,d[i].y,mid);
     89         insert(d[i].y,d[i].x,mid);
     90     }
     91 }
     92 inline bool jud(int mid)
     93 {
     94     rebuild(mid);
     95     ans=0;while (bfs())ans+=dfs(S,inf);
     96     return ans==k;
     97 }
     98 inline void work()
     99 {
    100     n=read();m=read();k=read();
    101     for (int i=1;i<=k;i++)c[i]=read();
    102     for (int i=1;i<=m;i++)
    103     {
    104         d[i].x=read();
    105         d[i].y=read();
    106     }
    107     int l=1,r=k,col=k;
    108     while (l<=r)
    109     {
    110         int mid=(l+r)>>1;
    111         if (jud(mid))col=mid,r=mid-1;
    112         else l=mid+1;
    113     }
    114     printf("%d
    ",col);
    115 }
    116 int main()
    117 {
    118     int T=read();
    119     while (T--)work();
    120 }
    Spoj NETADMIN
  • 相关阅读:
    Python学习-第三天代码作业
    网络原理-1
    整理操作系统
    操作系统--开发语言--重点
    操作系统
    C#数组的声明方式
    string.Format对C#字符串格式化(转)
    C#中两个Form窗口之间的传值(父->子)(子->父) (转)
    opencv MatchTemplate()模板匹配寻找最匹配部分
    摄像头,图片筛选颜色
  • 原文地址:https://www.cnblogs.com/zhber/p/7283854.html
Copyright © 2011-2022 走看看