hdu,hduacm

來源:互聯網
上載者:User

hdu,hduacm

題意:給一個N行M列的數字矩陣的行和以及列和,每個元素的大小不超過K,問這樣的矩陣是否存在,是否唯一,唯一則求出各個元素N(1 ≤ N ≤ 400) , M(1 ≤ M ≤ 400), K(1 ≤ K ≤ 40)。

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=4888

——>>建圖:

1)超級源S = 0,超級匯T = N + M + 1;

2)S到每個行和各連一條邊,容量為該行行和;

3)每個行和到每個列和各連一條邊,容量為K;

4)每個列和到 T 各連一條邊,容量為該列列和。

一個行到所有列連邊,為的是讓該行分流多少給各個列,正是該行某列元素的大小。。

所以,如果 S 到 T 的最大流 == 所有元素的和,則說明有解。。

殘量網路中的行列結點之間如果有長度 > 2 的環(自環長度為2,但無法調整流量),則說明這個環中的流量可以調整,使得達到最大流時該環上的流量不唯一,即矩陣不唯一。。

#include <cstdio>#include <cstring>#include <queue>#include <algorithm>using std::min;using std::queue;const int MAXN = 400 * 2 + 10;const int MAXM = 400 * 400 + 2 * MAXN;const int INF = 0x3f3f3f3f;struct EDGE{    int to;    int cap;    int flow;    int nxt;} edge[MAXM << 1];int N, M, K;int sum;int S, T;int hed[MAXN], ecnt;int cur[MAXN], h[MAXN];bool impossible, bUnique;void Init(){    impossible = false;    bUnique = true;    ecnt = 0;    memset(hed, -1, sizeof(hed));}void AddEdge(int u, int v, int cap){    edge[ecnt].to = v;    edge[ecnt].cap = cap;    edge[ecnt].flow = 0;    edge[ecnt].nxt = hed[u];    hed[u] = ecnt++;    edge[ecnt].to = u;    edge[ecnt].cap = 0;    edge[ecnt].flow = 0;    edge[ecnt].nxt = hed[v];    hed[v] = ecnt++;}bool Bfs(){    memset(h, -1, sizeof(h));    queue<int> qu;    qu.push(S);    h[S] = 0;    while (!qu.empty())    {        int u = qu.front();        qu.pop();        for (int e = hed[u]; e != -1; e = edge[e].nxt)        {            int v = edge[e].to;            if (h[v] == -1 && edge[e].cap > edge[e].flow)            {                h[v] = h[u] + 1;                qu.push(v);            }        }    }    return h[T] != -1;}int Dfs(int u, int cap){    if (u == T || cap == 0) return cap;    int flow = 0, subFlow;    for (int e = cur[u]; e != -1; e = edge[e].nxt)    {        cur[u] = e;        int v = edge[e].to;        if (h[v] == h[u] + 1 && (subFlow = Dfs(v, min(cap, edge[e].cap - edge[e].flow))) > 0)        {            flow += subFlow;            edge[e].flow += subFlow;            edge[e ^ 1].flow -= subFlow;            cap -= subFlow;            if (cap == 0) break;        }    }    return flow;}int Dinic(){    int maxFlow = 0;    while (Bfs())    {        memcpy(cur, hed, sizeof(hed));        maxFlow += Dfs(S, INF);    }    return maxFlow;}void Read(){    int r, c;    int rsum = 0, csum = 0;    S = 0;    T = N + M + 1;    for (int i = 1; i <= N; ++i)    {        scanf("%d", &r);        rsum += r;        AddEdge(S, i, r);    }    for (int i = 1; i <= M; ++i)    {        scanf("%d", &c);        csum += c;        AddEdge(i + N, T, c);    }    if (rsum != csum)    {        impossible = true;        return;    }    sum = rsum;    for (int i = 1; i <= N; ++i)    {        for (int j = M; j >= 1; --j)        {            AddEdge(i, j + N, K);        }    }}void CheckPossible(){    if (impossible) return;    if (Dinic() != sum)    {        impossible = true;    }}bool vis[MAXN];bool CheckCircle(int x, int f){    vis[x] = true;    for (int e = hed[x]; e != -1; e = edge[e].nxt)    {        if (edge[e].cap > edge[e].flow)        {            int v = edge[e].to;            if (v == f) continue;            if (vis[v]) return true;            else            {                if (CheckCircle(v, x)) return true;            }        }    }    vis[x] = false;    return false;}void CheckUnique(){    if (impossible) return;    memset(vis, 0, sizeof(vis));    for (int i = 1; i <= N; ++i)    {        if (CheckCircle(i, -1))        {            bUnique = false;            return;        }    }}void Output(){    if (impossible)    {        puts("Impossible");    }    else if (!bUnique)    {        puts("Not Unique");    }    else    {        puts("Unique");        for (int i = 1; i <= N; ++i)        {            for (int e = hed[i], j = 1; e != -1 && j <= M; e = edge[e].nxt, ++j)            {                printf("%d", edge[e].flow);                if (j < M)                {                    printf(" ");                }            }            puts("");        }    }}int main(){    while (scanf("%d%d%d", &N, &M, &K) == 3)    {        Init();        Read();        CheckPossible();        CheckUnique();        Output();    }    return 0;}



hdu acm 2032

我發我的ac代碼給你看吧,肯定是輸出時多了什麼空格什麼的,好好看看吧,加油
#include<stdio.h>
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int a[31][31];
int i,j,k;
for(i=1;i<=30;i++)
a[i][1]=a[i][i]=1;
if(n==1)printf("1\n");
if(n>=2)printf("1\n1 1\n");
for(i=3;i<=n;i++)
{
printf("1 ");
for(j=2;j<i;j++)
{
a[i][j]=a[i-1][j]+a[i-1][j-1];
printf("%d ",a[i][j]);
}
printf("%d\n",a[i][i]);
}
printf("\n");
}
}
 
HDU2001怎做

#include <stdio.h>
#include <math.h>
int main()
{
double x1,y1,x2,y2;
while(EOF != scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2))
{
printf("%.2lf\n",sqrt(pow(x1-x2,2)+pow(y1-y2,2)));
}
return 0;
}
 

聯繫我們

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