AOJ 2251 Merry Christmas (最小點覆蓋)

來源:互聯網
上載者:User

標籤:printf   bool   dfs   div   href   連線   print   mat   lin   

 

【題目連結】 http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2251

 

【題目大意】

  給出一張圖,現在有一些任務,要求在ti時刻送禮物到pi地點
  問至少需要幾個聖誕老人才能準時完成任務

 

【題解】

  我們將送完一個禮物之後還能趕到下一個地點送禮物的兩個點之間連線
  只要求出這個圖的最小點覆蓋就是答案

 

【代碼】

#include <cstdio>#include <algorithm>#include <cstring>#include <vector> #include <queue>using namespace std;const int MAX_V=2000;int V,match[MAX_V];vector<int> G[MAX_V];bool used[MAX_V];void add_edge(int u,int v){    G[u].push_back(v);    G[v].push_back(u);}bool dfs(int v){    used[v]=1;    for(int i=0;i<G[v].size();i++){        int u=G[v][i],w=match[u];        if(w<0||!used[w]&&dfs(w)){            match[v]=u;            match[u]=v;            return 1;        }    }return 0;}int bipartite_matching(){    int res=0;    memset(match,-1,sizeof(match));    for(int v=0;v<V;v++){        if(match[v]<0){            memset(used,0,sizeof(used));            if(dfs(v))res++;        }    }return res;}void clear(){for(int i=0;i<V;i++)G[i].clear();}const int MAX_N=100;const int MAX_L=1000;const int INF=0x3f3f3f3f;int N,M,L,x,y,z;int d[MAX_N][MAX_N],p[MAX_L],t[MAX_L];void init(){    V=2*L;    clear();    memset(d,INF,sizeof(d));    for(int i=0;i<M;i++){        scanf("%d%d%d",&x,&y,&z);        d[x][y]=d[y][x]=z;    }    for(int i=0;i<L;i++)scanf("%d%d",&p[i],&t[i]);    for(int k=0;k<N;k++){        d[k][k]=0;        for(int i=0;i<N;i++)            for(int j=0;j<N;j++)                d[i][j]=min(d[i][j],d[i][k]+d[k][j]);    }}void solve(){    for(int i=0;i<L;i++)        for(int j=0;j<L;j++){            if(i!=j&&t[i]+d[p[i]][p[j]]<=t[j])add_edge(i<<1,j<<1|1);        }    printf("%d\n",L-bipartite_matching());}int main(){    while(~scanf("%d%d%d",&N,&M,&L),N){        init();        solve();    }return 0;}

AOJ 2251 Merry Christmas (最小點覆蓋)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.