Image copy detection HDU - 3523 最小费用流

The success of the Internet and cost-effective digital storage device has made it possible to replicate, transmit, and distribute digital content in an effortless way. Thus, the protection of intellectual Property right (IPR) has become a crucial legal issue. Detecting copies of digital media(images, audio and video) is a basic requirement for IPR protection (or copyright protection).The applications of copydetection include usage tracking and copyright violation enforcement.
For those above purposes, the image copy detection system came out.It aggregated all the images which were viewed as a copy of original image.In this system,an image is partitioned into m×n equalsized blocks,which makes the system independent of input image sizes,and an m×n sub-image is calculated by taking the average value of each block(see Figure.1(b)), This array is converted to a rank matrix as shown in Fig.1(c).Suppose that the intensity values in Fig.1(b) are changed in the copied image so that its sub-image has values:{{30, 60, 40}, {70, 90, 110}, {50, 100, 80}}. Nevertheless,its rank matrix is identical to that shown in Fig. 1(c) and thus perfect matching with original image can be achieved.



Fig.1 (a)An image is divided into m×n blocks (3×3 in this example),(b)average values of blocks, and(c)rank matrix of (b). Let T and Q represent test image and original image,N represent the matrix size;there exists N tuples (t1,q1),…,( tn,qn),…,( tN,qN)(the order of the rank matrix:from left to right and from top to bottom).Now we define D(T,Q) = measures the distance between the two images(it’s obvious that if D(T,Q) gets smaller while the probability of the test image is considered as a copy of original image by the system becomes larger). Since there are M original images in the image copy detection system(Q1…QM). And the distance between T and some original images is given by D(T,Q1…QM)= To make it simple,we want to find an image which owns least D(T,Q1…QM).

Input The first line of input should give the number of cases, T (at most 100). T test cases follow. The first line of each test case contains two integer n (1 ≤ n ≤ 100) and m(1 ≤ m ≤ 100) indicating the size of rank matrix and number of original images. The following m lines each contains a sequence of n different integers denotes the rank matrix. Output For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y is the least distance D(T,Q1…QM).
Sample Input
2
3 2
1 2 3
1 3 2
9 3
1 4 2 5 7 9 3 8 6
2 1 4 5 9 3 7 6 8
9 8 7 6 5 4 3 2 1
Sample Output
Case #1: 2
Case #2: 58




#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int M = 1e3 + 10;
typedef pair<int,int > P;//first保存最短距离,second保存顶点编号
//表示边的结构体(终点、容量、费用、反向边)
struct edge
{
    int to,cap,cost,rev;
    //edge();
   // edge(int a,int b,int c,int d){to=a;cap=b;cost=c;rec=d;};
};
int t;
int V;//顶点数
int n,m;
vector<edge>G[M];
int h[M],dis[M];//图的势(指的是每个顶点赋予的一个标号h(v)) 最短距离
int prevv[M],preve[M]; //图的最短路中的前驱节点和对应的边
//增加一条从a到b容量为c费用为d的一条边
void add_edge(int a,int b,int c,int d)
{
    G[a].push_back(edge{b,c,d,G[b].size()});
    G[b].push_back(edge{a,0,-d,G[a].size()-1});
}
//求解从s到t流量为F的最小费用流
// 如果没有流量为F 的流,输出为-1
int min_cost_flow(int st,int en,int f){
    int ans=0;
    memset(h,0,sizeof(h));
    while(f>0){
        priority_queue<P,vector<P>,greater<P> >que;
        for(int i=0;i<M;i++) dis[i]=inf;
        dis[st]=0;que.push(P(0,st));
        while(!que.empty()){
            P p=que.top();que.pop();
            int v=p.second;
            if(dis[v]<p.first) continue;
            for(unsigned int i=0;i<G[v].size();i++){
                edge &e=G[v][i];
                if(e.cap>0&&dis[e.to]>dis[v]+e.cost+h[v]-h[e.to]){
                    dis[e.to]=dis[v]+e.cost+h[v]-h[e.to];
                    prevv[e.to]=v;
                    preve[e.to]=i;
                    que.push(P(dis[e.to],e.to));
                }
            }
        }
        if(dis[en]==inf) return -1;
        for(int i=0;i<M;i++) h[i]+=dis[i];
        int d=f;
        for(int i=en;i!=st;i=prevv[i]){
            d=min(d,G[prevv[i]][preve[i]].cap);
        }
        f-=d;
        ans+=d*h[en];
        for(int i=en;i!=st;i=prevv[i]){
            edge &e=G[prevv[i]][preve[i]];
            e.cap-=d;
            G[i][e.rev].cap+=d;
        }
    }
    return ans;
}
int num[110][110],tmp[110][110];
int main()
{
    scanf("%d",&t);
    int cas=1;
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0;i<M;i++)
            G[i].clear();
        for(int i=1;i<=m;i++)
            for(int j=1;j<=n;j++)
            scanf("%d",&num[i][j]);
        for(int i=1;i<=n;i++)
            add_edge(0,i,1,0);
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                int sum=0;
                for(int k=1;k<=m;k++) sum+=abs(num[k][j]-i);
                tmp[j][i]=sum;
            }
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                add_edge(i,n+j,1,tmp[j][i]);
            }
        }
        for(int i=1;i<=n;i++) add_edge(n+i,2*n+1,1,0);
        int ans=min_cost_flow(0,2*n+1,n);
        printf("Case #%d: %d\n",cas++,ans);
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/liluoyu_1016/article/details/80086723