STl全排列应用 HDU 2464

A Pair of Graphs

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 739    Accepted Submission(s): 394


Problem Description
We say that two graphs are equivalent if and only if a one-to-one correspondence between their nodes can be established and under such a correspondence their edges are exactly the same. Given A and B, two undirected simple graphs with the same number of vertexes, you are to find a series of operations with the minimum cost that will make the two graphs equivalent. An operation may be one of the following two types:
a) Add an arbitrary edge (x, y), provided that (x, y) does not exist before such an operation. Such an operation costs I A and I B on two graphs, respectively.
b) Delete an existing edge (x, y), which costs D A and D B on two graphs,respectively.
 

Input
There are multiple test cases in the input file.
Each test case starts with three integers, N, M A and M B, ( 1 ≤ N ≤ 8, 0 ≤ M B ,M A ≤ N*(N-1)/2 ), the total number of vertexes, the number of edges in graph A,and the number of edges in graph B, respectively. Four integers I A, I B, D A, and D B come next, (0 ≤ I A, I B, D A, D B ≤ 32767 ), representing the costs as stated in the problem description. The next M A + M B lines describe the edges in graph A followed by those in graph B. Each line consists of exactly two integers, X and Y ( X ≠ Y , 0 ≤ X,Y < N).
Two successive test cases are separated by a blank line. A case with N = 0, M A = 0,and M B = 0 indicates the end of the input file, and should not be processed by your program.
 

Output
Please print the minimum cost in the format as illustrated below.
 

Sample Input
  
  
1 0 0 1 2 3 7 4 2 3 1 6 5 1 0 1 0 3 0 2 1 2 1 0 0 0 0
 

Sample Output
  
  
Case #1: 0 Case #2: 1
 
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <ctype.h>
#include <limits.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <deque>
#include <vector>
#include <set>
#include <map>
using namespace std;
#define MAXN 10
int marka[MAXN][MAXN];
int markb[MAXN][MAXN];
int cost[MAXN];
int biao[MAXN];

int min1(int a,int b){
    return a<b?a:b;
}

int main(){
    int n,ma,mb;
    int i,j,ti,tj;
    int a,b;
    int cas = 1;

    while(~scanf("%d%d%d",&n,&ma,&mb)){
        if(n==0 && ma==0 && mb==0){
            break;
        }
        memset(marka,0,sizeof(marka));
        memset(markb,0,sizeof(markb));
        memset(biao,0,sizeof(biao));
        for(i=0;i<4;i++){
            scanf("%d",&cost[i]);
        }
        for(i=0;i<ma;i++){
            scanf("%d%d",&a,&b);
            marka[a][b] = 1;
            marka[b][a] = 1;
        }
        for(i=0;i<mb;i++){
            scanf("%d%d",&a,&b);
            markb[a][b] = 1;
            markb[b][a] = 1;
        }
        int ans = -1;
        int tmp;
        for(i=0;i<n;i++){
            biao[i] = i;
        }
        do{
            tmp = 0;
            for(i=0;i<n;i++){
                for(j=0;j<n;j++){
                    //tj = biao[j];
                    //ti = biao[i];
                    if(j <= i){
                        continue;
                    }
                    tj = biao[j];
                    ti = biao[i];
                    if(marka[i][j]==1 && markb[ti][tj]==0){
                        tmp+=min1(cost[2],cost[1]);
                    }
                    if(marka[i][j]==0 && markb[ti][tj]==1){
                        tmp+=min1(cost[0],cost[3]);
                    }
                }
            }
            if(ans == -1){
                ans = tmp;
            }
            else{
                ans = min1(ans,tmp);
            }
        }while(next_permutation(biao,biao+n));//这样就能够将所有的情况都考虑进去
        printf("Case #%d: %d\n",cas,ans);
        cas++;
    }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值