• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            2012sdACM省賽 題目H

            http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2414


            An interesting game

            Time Limit: 2000MS Memory limit: 65536K

            題目描述

            Xiao Ming recently designs a little game, in front of player there are N small hillsides put in order, now Xiao Ming wants to increase some hillsides to block the player, so he prepared another M hillsides, but he does not hope it will be too difficult,so only K in M hillsides are selected to add at most. Paying attention to the original N hillsides, between each two can add only one hillside. Xiao Ming expects players from the starting place to reach the destination in turn and passes all the hillsides to make his distance travelled longest. Please help Xiao Ming how to add the hillsides that he prepared. Note: The distance between two hillsides is the absolute value of their height difference.

            輸入

            The first line of input is T, (1 <= T <= 100) the number of test cases. Each test case starts with three integers N,M,K (2 <= N <= 1000, 1 <= M <= 1000, 1 <= K <= M and 1 <= K < N), which means that the number of original hillsides, the number of hillsides Xiao Ming prepared and The number of most Xiao Ming can choose from he prepared. Then follow two lines, the first line contains N integers Xi (0 <= Xi <= 30), denoting the height of each original hillside, Note: The first integer is player's starting place and the last integer is player's destination. The second line contains M integers Yi (0 <= Yi <= 30), denoting the height of prepared each hillsides.

            輸出

            For every test case, you should output "Case k: " first in a single line, where k indicates the case number and starts from 1. Then print the distance player can travel longest.

            示例輸入

            3 2 1 1 6 9 8 2 1 1 6 9 15 3 2 1 5 9 15 21 22 

            示例輸出

            Case 1: 3 Case 2: 15 Case 3: 36 

            提示

             

            來(lái)源

             2012年"浪潮杯"山東省第三屆ACM大學(xué)生程序設(shè)計(jì)競(jìng)賽

            示例程序


            比賽的時(shí)候沒(méi)有往圖這方面考慮

            比賽結(jié)束后發(fā)現(xiàn)是匹配問(wèn)題,然后就從這開(kāi)始,一直悲劇了

            這個(gè)題目我寫了兩種解法

            1,構(gòu)造二分圖,求二分圖的最大權(quán)匹配,然后貪掉小的邊,復(fù)雜度 (n^3)超時(shí)
            2,最大費(fèi)用可行流,加一條限制邊,變?yōu)樽钚≠M(fèi)用流問(wèn)題

            兩個(gè)模型不寫了,這幾天搞這個(gè)題無(wú)力了
            貼下代碼,小小的參考了下段神的代碼,
            本來(lái)寫的是鄰接矩陣的,結(jié)果無(wú)奈到死,一直改不出來(lái),我想是不是那個(gè)沒(méi)法處理負(fù)權(quán)邊呢?我也不知道 是不是
            然后就改成了鄰接表的
            代碼1 KM
            #include<stdio.h>
            #include
            <string.h>
            #include
            <math.h>
            #include
            <algorithm>
            #define pp printf("here\n")
            #define maxn 1005
            #define inf 0xfffffff
            using namespace std;
            int map[maxn][maxn],w[maxn][maxn],f[maxn],a[maxn];
            int b[maxn],x[maxn],y[maxn],pre[maxn];
            bool visx[maxn],visy[maxn];
            int n,m,k,num;
            int ans,slack;
            int count1=0;
            int lx[maxn],ly[maxn];
            int maty[maxn],matx[maxn];
            int fx[maxn],fy[maxn];
            int nx,ny;
            struct node
            {
                
            int u,val;
            } edge[maxn];
            int abs1(int x)
            {
                
            if(x<0return -x;
                
            else return x;
            }
            int cmp(node t1,node t2)
            {
                
            return t1.val<t2.val;
            }
            int path(int u)
            {
                fx[u] 
            = 1;
                
            for (int v = 1; v <= ny; v++)
                    
            if (lx[u] + ly[v] == w[u][v] && fy[v] < 0)
                    {
                        fy[v] 
            = 1;
                        
            if (maty[v] < 0 || path(maty[v]))
                        {
                            matx[u] 
            = v;
                            maty[v] 
            = u;
                            
            return 1;
                        }
                    }
                
            return 0;
            }
            int km()
            {
                
            int i,j,k,ret = 0;
                memset(ly, 
            0sizeof(ly));
                
            for (i = 1; i <= nx; i++)
                {
                    lx[i] 
            = -inf;
                    
            for (j = 1; j <= ny; j++)
                        
            if (w[i][j] > lx[i]) lx[i] = w[i][j];
                }
                memset(matx, 
            -1sizeof(matx));
                memset(maty, 
            -1sizeof(maty));
                
            for (i = 1; i <= nx; i++)
                {
                    memset(fx, 
            -1sizeof(fx));
                    memset(fy, 
            -1sizeof(fy));
                    
            if (!path(i))
                    {
                        i
            --;
                        
            int p = inf;
                        
            for (k = 1; k <= nx; k++)
                        {
                            
            if (fx[k] > 0)
                                
            for (j = 1; j <= ny; j++)
                                    
            if (fy[j] < 0 && lx[k] + ly[j] - w[k][j] < p)
                                        p
            =lx[k]+ly[j]-w[k][j];
                        }
                        
            for (j = 1; j <= ny; j++)
                            ly[j] 
            += fy[j]<0 ? 0 : p;
                        
            for (j = 1; j <= nx; j++)
                            lx[j] 
            -= fx[j]<0 ? 0 : p;
                    }
                }
            }
            void work()
            {
                
            int i,j;
                num
            =0;
                
            for(i=1; i<=m; i++)
                {
                    edge[num].u
            =pre[i];
                    edge[num].val
            =map[maty[i]][i];
                    ans
            +=edge[num].val;
                    num
            ++;
                }
                sort(edge,edge
            +num,cmp);
                
            //for(i=0;i<num;i++)
                    
            //printf("%d ",edge[i].val);printf("\n");
                
            //for(i=0;i<num;i++) printf("%d  %d\n",edge[i].u,edge[i].val);
                for(i=0; i<m-k; i++)
                    ans
            =ans-edge[i].val;
            }
            int main()
            {
                
            int times,t,i,j,w;
                scanf(
            "%d",&t);
                
            for(times=1; times<=t; times++)
                {
                    scanf(
            "%d%d%d",&n,&m,&k);
                    memset(a,
            0,sizeof(a));
                    memset(map,
            0,sizeof(map));
                    memset(f,
            0,sizeof(f));
                    ans
            =0;
                    
            for(i=1; i<=n; i++)
                    {
                        scanf(
            "%d",&a[i]);
                        
            if(i!=1)
                        {
                            f[i
            -1]=abs1(a[i]-a[i-1]);
                            ans
            +=f[i-1];
                        }
                    }
                    
            for(i=1; i<=m; i++) scanf("%d",&b[i]);
                    
            //printf("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n");
                    for(i=1; i<=n-1; i++)
                        
            for(j=1; j<=m; j++)
                        {
                            w
            =abs1(a[i]-b[j])+abs1(a[i+1]-b[j])-f[i];
                            map[i][j]
            =w;
                            
            //printf("%d %d %d\n",i,j,w);
                        }
                    
            //printf("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n");
                    nx=n-1;
                    ny
            =m;
                    km();
                    work();
                    printf(
            "Case %d: %d\n",times,ans);
                }
                
            return 0;
            }

            2 最小費(fèi)用流,(最小費(fèi)用路的做法,spfa+mincost)
            #include<cstdio>
            #include
            <cstring>
            #define maxn 1500
            #define maxm 150000
            #define inf 0x7fffffff
            int h[maxn],a[50],f[maxn];
            int n,m,k,s,t;
            int sum,ans,nn;
            bool vis[maxn];
            bool mmm;
            int pre[maxn];
            struct node 
            {
                
            int v,next,cap,cost;
            }edge[maxm
            +1];
            int head[maxn],num;
            void add(int u,int v,int cap,int cost)
            {
                edge[num].v
            =v;
                edge[num].cap
            =cap;
                edge[num].cost
            =cost;
                edge[num].next
            =head[u];
                head[u]
            =num++;
                edge[num].v
            =u;
                edge[num].cap
            =0;
                edge[num].cost
            =-cost;
                edge[num].next
            =head[v];
                head[v]
            =num++;
            }
            int abs(int x)
            {
                
            if(x<0return -x;
                
            return x;
            }
            int min(int a,int b)
            {
                
            return a<b?a:b;
            }
            bool spfa()
            {
                
            int q[maxm+1],head1,tail,dist[maxn],i;
                memset(dist,
            0x6f,sizeof(dist));
                memset(vis,
            0,sizeof(vis));
                head1
            =0;tail=1;
                q[tail]
            =s;
                vis[s]
            =1;
                dist[s]
            =0;
                
            while(head1!=tail)
                {
                    head1
            =head1%maxm+1;
                    
            int u=q[head1];
                    
            for(i=head[u];i!=-1;i=edge[i].next)
                    {
                        
            int v=edge[i].v;
                        
            if(dist[v]>dist[u]+edge[i].cost&&edge[i].cap>0)
                        {
                            dist[v]
            =dist[u]+edge[i].cost;
                            pre[v]
            =i;
                            
            if(!vis[v])
                            {
                                tail
            =tail%maxm+1;
                                q[tail]
            =v;
                                vis[v]
            =1;
                            }
                        }
                    }
                    vis[u]
            =1;
                }
                
            if(dist[t]<0return true;
                
            else return false;
            }
            void mincost()
            {
                
            int i;
                i
            =t;
                
            while(i!=s)
                {
                    ans
            +=edge[pre[i]].cost;
                    edge[pre[i]].cap
            --;
                    
            if(edge[pre[i]].cap==0) mmm=true;
                    edge[pre[i]
            ^1].cap++;
                    
            if(edge[pre[i]^1].cap==1) mmm=true;
                    i
            =edge[pre[i]^1].v;
                }
            }
            void work()
            {
                ans
            =0;
                mmm
            =true;
                
            while(!mmm||spfa())
                {
                    mmm
            =false;
                    mincost();
                }
                sum
            =sum-ans;
            }
            int main()
            {
                
            int times,i,j,t1,x;
                scanf(
            "%d",&t1);
                
            for(times=1; times<=t1; times++)
                {
                    scanf(
            "%d%d%d",&n,&m,&k);
                    sum
            =0;
                    
            for(i=1; i<=n; i++)
                    {
                        scanf(
            "%d",&h[i]);
                        
            if(i!=1)
                        {
                            f[i
            -1]=abs(h[i]-h[i-1]);
                            sum
            =sum+f[i-1];
                        }
                    }
                    memset(a,
            0,sizeof(a));
                    
            for(i=1; i<=m; i++)
                    {
                        scanf(
            "%d",&x);
                        a[x]
            ++;
                    }
                    s
            =0;
                    nn
            =33+n;
                    
            //源匯2個(gè),k限制1個(gè),附加節(jié)點(diǎn)31(0-30),兩相鄰h的合并頂點(diǎn)n-1 總共33+n個(gè)節(jié)點(diǎn)
                    t=32+n;
                    
            //////構(gòu)圖
                    num=0;
                    memset(head,
            -1,sizeof(head));
                    add(
            0,1,k,0);
                    
            for(i=0; i<=30; i++)
                    {
                        add(
            1,i+2,a[i],0);
                    }
                    
            for(i=0; i<=30; i++)
                        
            if(a[i]!=0)
                            
            for(j=1; j<=n-1; j++)
                                
            if(f[j]<(abs(h[j]-i)+abs(h[j+1]-i)))
                                {
                                    
            int ww=f[j]-(abs(h[j]-i)+abs(h[j+1]-i));//設(shè)為負(fù)權(quán),求最小費(fèi)用流
                                    add(i+2,j+32,1,ww);
                                }
                    
            for(i=1; i<=n-1; i++)
                    {
                        add(i
            +32,t,1,0);
                    }
                    work();
                    printf(
            "Case %d: %d\n",times,sum);
                }
                
            return 0;
            }

            posted on 2012-05-17 17:16 jh818012 閱讀(180) 評(píng)論(0)  編輯 收藏 引用


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            <2025年7月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿

            文章檔案(85)

            搜索

            最新評(píng)論

            • 1.?re: poj1426
            • 我嚓,,輝哥,,居然搜到你的題解了
            • --season
            • 2.?re: poj3083
            • @王私江
              (8+i)&3 相當(dāng)于是 取余3的意思 因?yàn)?3 的 二進(jìn)制是 000011 和(8+i)
            • --游客
            • 3.?re: poj3414[未登錄](méi)
            • @王私江
              0ms
            • --jh818012
            • 4.?re: poj3414
            • 200+行,跑了多少ms呢?我的130+行哦,你菜啦,哈哈。
            • --王私江
            • 5.?re: poj1426
            • 評(píng)論內(nèi)容較長(zhǎng),點(diǎn)擊標(biāo)題查看
            • --王私江
            久久亚洲中文字幕精品一区| 久久天天躁夜夜躁狠狠| 99久久久精品| 久久久久久久综合日本亚洲| 99久久婷婷国产综合精品草原| 久久免费大片| 久久99精品国产自在现线小黄鸭| 久久99国产乱子伦精品免费| 91久久九九无码成人网站| 亚洲国产成人精品无码久久久久久综合| 性做久久久久久久久久久| 亚洲中文久久精品无码| 大美女久久久久久j久久| 亚洲熟妇无码另类久久久| 久久精品成人免费国产片小草| 97精品依人久久久大香线蕉97| 国产福利电影一区二区三区久久久久成人精品综合 | 亚洲va中文字幕无码久久| 一本色道久久88加勒比—综合| 99精品国产免费久久久久久下载| 成人资源影音先锋久久资源网| 久久99久国产麻精品66| 久久乐国产精品亚洲综合| 国产精品日韩欧美久久综合| 精品久久无码中文字幕| 久久人妻无码中文字幕| 久久久精品久久久久久 | 国产精品免费久久久久久久久| 久久精品这里热有精品| 久久精品aⅴ无码中文字字幕重口| 伊人精品久久久久7777| 青青草原综合久久大伊人导航| 久久精品国产久精国产| 久久久精品国产sm调教网站 | 老司机国内精品久久久久| 国内精品久久人妻互换| 久久亚洲美女精品国产精品| 久久www免费人成看片| 蜜臀av性久久久久蜜臀aⅴ| 日韩人妻无码一区二区三区久久| 亚洲精品乱码久久久久久自慰|