【poj2699】 The Maximum Number of Strong Kings

來源:互聯網
上載者:User

標籤:題意   link   namespace   ++   out   dinic   相等   algo   注意   

http://poj.org/problem?id=2699 (題目連結)

題意

  給出1張有向完全圖。U->V表示U可以打敗V並得一分。如果一個人的得分最高,或者他打敗所有比自己得分高的人,那麼此人就是king。現在按順序給出每個人的得分,求最多可能有多少個king同時存在。

Solution

  想了半天貪心,然而得分相等的情況真的很不好處理。。真的沒想到是最大流。。左轉題解:http://blog.csdn.net/sdj222555/article/details/7797257

  考慮這樣建圖的正確性。

  借用題解的example,假設序列長成這樣:1....i......n。那麼i不是king有以下這幾種情況

  1.i的得分少於得分比它大的人的個數

  這種情況顯然i不可能贏所有得分比它大的人,那麼這如何在我們所構建的圖上體現呢?

  對於i與得分比i大的人的比賽,從i連向它們,顯然,這些邊不可能滿流,因為i不可能贏這麼多場,於是不成立。

  2.n已經無法給予i贏的機會

  因為得分比i大的人想要成為King,必須贏得得分比他們更大的人n,而n能夠輸的場次是有限的。

  如果從i連向(i,n)的比賽使i強行贏得勝利,會使得邊(s,n)不滿流,於是不成立。

  也許還有別的情況我沒考慮到,唉最近思維僵化,沒救了,如果有補充請提出╮(╯_╰)╭

細節

  多組資料注意初始化,為什麼我還是要16ms。。內建常數。。

代碼
// poj2699#include<algorithm>#include<iostream>#include<cstdlib>#include<cstring>#include<cstdio>#include<cmath>#include<queue>#define LL long long#define inf 2147483640#define Pi acos(-1.0)#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);using namespace std;const int maxn=1010;struct edge {int to,next,w;}e[maxn];int head[maxn],d[maxn],f[20][20],a[maxn],Max;int n,m,ans,cnt=1,es,et;char ch[maxn];void link(int u,int v,int w) {e[++cnt]=(edge){v,head[u],w};head[u]=cnt;e[++cnt]=(edge){u,head[v],w};head[v]=cnt;}void read() {gets(ch);n=m=Max=0;int l=strlen(ch);for (int i=0;i<l;i++) if (ch[i]>=‘0‘ && ch[i]<=‘9‘) {m=m*10+ch[i]-‘0‘;if (i==l || ch[i+1]<‘0‘ || ch[i+1]>‘9‘) a[++n]=m,m=0,Max=max(Max,a[n]);}for (int i=1;i<=n;i++)for (int j=i+1;j<=n;j++) f[i][j]=f[j][i]=++m;es=n+m+1;et=es+1;}void Init() {cnt=ans=0;memset(head,0,sizeof(head));}bool bfs() {memset(d,-1,sizeof(d));queue<int> q;q.push(es);d[es]=0;while (!q.empty()) {int x=q.front();q.pop();for (int i=head[x];i;i=e[i].next) if (e[i].w && d[e[i].to]<0) {d[e[i].to]=d[x]+1;q.push(e[i].to);}}return d[et]>0;}int dfs(int x,int f) {if (x==et || f==0) return f;int used=0,w;for (int i=head[x];i;i=e[i].next) if (e[i].w && d[e[i].to]==d[x]+1) {w=dfs(e[i].to,min(e[i].w,f-used));used+=w;e[i].w-=w;e[i^1].w+=w;if (used==f) return used;}if (!used) d[x]=-1;return used;}void Dinic() {while (bfs()) ans+=dfs(es,inf);}int main() {int T;scanf("%d",&T);getchar();while (T--) {read();for (int s=1;s<=n;s++) {Init();for (int i=1;i<=n;i++) link(es,i,a[i]);for (int i=1;i<s;i++)for (int j=i+1;j<=n;j++) link(i,f[i][j]+n,1),link(j,f[i][j]+n,1);for (int i=s;i<=n;i++)for (int j=i+1;j<=n;j++) {link(i,f[i][j]+n,1);if (a[i]==Max) link(j,f[i][j]+n,1);}for (int i=n+1;i<=n+m;i++) link(i,et,1);Dinic();if (ans==m) {printf("%d\n",n-s+1);break;}}}return 0;}

  

【poj2699】 The Maximum Number of Strong Kings

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.