2008 Asia Regional Beijing (网络流+树状数组+贪心)hdu2485 - 2494

Destroying the bus stations

Problem Description

Gabiluso is one of the greatest spies in his country. Now he’s trying to complete an “impossible” mission —– to make it slow for the army of City Colugu to reach the airport. City Colugu has n bus stations and m roads. Each road connects two bus stations directly, and all roads are one way streets. In order to keep the air clean, the government bans all military vehicles. So the army must take buses to go to the airport. There may be more than one road between two bus stations. If a bus station is destroyed, all roads connecting that station will become no use. What’s Gabiluso needs to do is destroying some bus stations to make the army can’t get to the airport in k minutes. It takes exactly one minute for a bus to pass any road. All bus stations are numbered from 1 to n. The No.1 bus station is in the barrack and the No. n station is in the airport. The army always set out from the No. 1 station.
No.1 station and No. n station can’t be destroyed because of the heavy guard. Of course there is no road from No.1 station to No. n station.

Please help Gabiluso to calculate the minimum number of bus stations he must destroy to complete his mission.

Input

There are several test cases. Input ends with three zeros.

For each test case:

The first line contains 3 integers, n, m and k. (0< n <=50, 0< m<=4000, 0 < k < 1000)
Then m lines follows. Each line contains 2 integers, s and f, indicating that there is a road from station No. s to station No. f.

Output

For each test case, output the minimum number of stations Gabiluso must destroy.

Sample Input

5 7 3
1 3
3 4
4 5
1 2
2 5
1 4
4 5
0 0 0

Sample Output

2
题意:N个点,M条边,求最小删除多少条边可以是最短路大于K
思路:把1~N的所有路径中,距离小于等于K的建图,然后拆点求最大流,求距离小于等于K的时候用floyed

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=210;
const int maxm=100010;
const int INF=0x3f3f3f3f;
int N,M,K;
int head[maxn],tot,nn;
int pre[maxn],dis[maxn];
bool vis[maxn];
int gap[maxn],cur[maxn];
int F[maxn][maxn];
struct node
{
    int v,next,f;
}edge[maxm*2];
void init()
{
    tot=0;
    memset(head,-1,sizeof(head));
    memset(pre,-1,sizeof(pre));
}
void add_edge(int x,int y,int f)
{
    edge[tot].v=y;
    edge[tot].f=f;
    edge[tot].next=head[x];
    head[x]=tot++;
    edge[tot].v=x;
    edge[tot].f=0;
    edge[tot].next=head[y];
    head[y]=tot++;
}
int SAP(int st,int en)
{
    for(int i=0;i<=nn;i++)
    {
        cur[i]=head[i];
        dis[i]=gap[i]=0;
    }
    int u=0;
    int flow=0,aug=INF;
    gap[st]=nn;
    u=pre[st]=st;
    bool flag;
    while(dis[st]<nn)
    {
        flag=0;
        for(int &j=cur[u];j!=-1;j=edge[j].next)
        {
            int v=edge[j].v;
            if(edge[j].f>0&&dis[u]==dis[v]+1)
            {
                flag=1;
                if(edge[j].f<aug)aug=edge[j].f;
                pre[v]=u;
                u=v;
                if(u==en)
                {
                    flow+=aug;
                    while(u!=st)
                    {
                        u=pre[u];
                        edge[cur[u]].f-=aug;
                        edge[cur[u]^1].f+=aug;
                    }
                    aug=INF;
                }
                break;
            }
        }

        if(flag)continue;
        int mindis=nn;
        for(int j=head[u];j!=-1;j=edge[j].next)
        {
            int v=edge[j].v;
            if(dis[v]<mindis&&edge[j].f>0)
            {
                mindis=dis[v];
                cur[u]=j;
            }
        }
        if((--gap[dis[u]])==0)break;
        gap[dis[u]=mindis+1]++;
        u=pre[u];
    }
    return flow;
}
void floyed()
{
    for(int k=1;k<=N;k++)
    for(int i=1;i<=N;i++)
        for(int j=1;j<=N;j++)
                if(F[i][k]<INF&&F[k][j]<INF&&i!=j)
                    F[i][j]=min(F[i][j],F[i][k]+F[k][j]);
}
int main()
{
    int u,v;
    while(scanf("%d%d%d",&N,&M,&K)!=EOF,N+M+K)
    {
        init();
        memset(F,INF,sizeof(F));
        for(int i=0;i<=N;i++)F[i][i]=0;
        for(int i=0;i<M;i++)
        {
            scanf("%d%d",&u,&v);
            F[u][v]=1;
        }
        floyed();
        for(int i=1;i<=N;i++)
        {
            for(int j=1;j<=N;j++)
            {
                if(i==j||F[i][j]>1)continue;
                if(F[1][i]==INF||F[j][N]==INF)continue;
                if(F[1][i]+F[j][N]<K)
                    add_edge(i+N,j,INF);
            }
        }
        add_edge(1,1+N,INF);
        for(int i=1;i<=N;i++)
            add_edge(i,i+N,1);
        add_edge(N,2*N,INF);
        nn=2*N+1;
        int ans=SAP(1,2*N);
        if(F[1][N]==INF)printf("0\n");
        else if(ans==INF)printf("%d\n",N);
        else printf("%d\n",ans);
    }
    return 0;
}

Priest John’s Busiest Day

Problem Description

John is the only priest in his town. October 26th is the John’s busiest day in a year because there is an old legend in the town that the couple who get married on that day will be forever blessed by the God of Love. This year N couples plan to get married on the blessed day. The i-th couple plan to hold their wedding from time Si to time Ti. According to the traditions in the town, there must be a special ceremony on which the couple stand before the priest and accept blessings. Moreover, this ceremony must be longer than half of the wedding time and can’t be interrupted. Could you tell John how to arrange his schedule so that he can hold all special ceremonies of all weddings?

Please note that:

John can not hold two ceremonies at the same time.
John can only join or leave the weddings at integral time.
John can show up at another ceremony immediately after he finishes the previous one.

Input

The input consists of several test cases and ends with a line containing a zero.

In each test case, the first line contains a integer N ( 1 ≤ N ≤ 100,000) indicating the total number of the weddings.

In the next N lines, each line contains two integers Si and Ti. (0 <= Si < Ti <= 2147483647)

Output

For each test, if John can hold all special ceremonies, print “YES”; otherwise, print “NO”.

Sample Input

3
1 5
2 4
3 6
2
1 5
4 6
0

Sample Output

NO
YES
贪心,按照中点排序,然后扫一遍

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
const LL maxn=200010;
const LL maxm=100010;
const LL INF=0x3f3f3f3f;
LL N;
struct node
{
    LL x,y,mid,len;
    bool operator<(const node &A)const
    {
        if(mid==A.mid) return x<A.x;
        return mid<A.mid;
    }
}a[maxn];
int main()
{
    while(scanf("%d",&N)!=EOF,N)
    {
        for(LL i=1;i<=N;i++)
        {
            scanf("%I64d%I64d",&a[i].x,&a[i].y);
            a[i].mid=a[i].x+a[i].y;
            a[i].len=(a[i].y-a[i].x)/2+1;
        }
        sort(a+1,a+1+N);
        bool flag=true;
        LL t=0;
        for(LL i=1;i<=N;i++)
        {
            if(t<=a[i].x) t=a[i].x+a[i].len;
            else if(t+a[i].len<=a[i].y) t=t+a[i].len;
            else
            {
                flag=false;
                break;
            }
        }
        printf("%s\n",flag?"YES":"NO");
    }
    return 0;
}

Ping pong

Problem Description

N(3<=N<=20000) ping pong players live along a west-east street(consider the street as a line segment).

Each player has a unique skill rank. To improve their skill rank, they often compete with each other. If two players want to compete, they must choose a referee among other ping pong players and hold the game in the referee’s house. For some reason, the contestants can’t choose a referee whose skill rank is higher or lower than both of theirs.

The contestants have to walk to the referee’s house, and because they are lazy, they want to make their total walking distance no more than the distance between their houses. Of course all players live in different houses and the position of their houses are all different. If the referee or any of the two contestants is different, we call two games different. Now is the problem: how many different games can be held in this ping pong street?

Input

The first line of the input contains an integer T(1<=T<=20), indicating the number of test cases, followed by T lines each of which describes a test case.

Every test case consists of N + 1 integers. The first integer is N, the number of players. Then N distinct integers a1, a2 … aN follow, indicating the skill rank of each player, in the order of west to east. (1 <= ai <= 100000, i = 1 … N).

Output

For each test case, output a single line contains an integer, the total number of different games.

Sample Input

1
3 1 2 3

Sample Output

1

树状数组的简单应用

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long LL;
const int maxn=100010;
int N;
LL tree[maxn];
struct node
{
    int x,id;
    bool operator<(const node &a)const
    {
        return x<a.x;
    }
}a[maxn];
int lowbit(int x)
{
    return x&(-x);
}
LL getsum(int x)
{
    LL res=0;
    while(x)
    {
        res+=tree[x];
        x-=lowbit(x);
    }
    return res;
}
void add(int x,int val)
{
    while(x<=N)
    {
        tree[x]+=val;
        x+=lowbit(x);
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&N);
        for(int i=1;i<=N;i++)
        {
            scanf("%d",&a[i].x);
            a[i].id=i;
        }
        stable_sort(a+1,a+1+N);
        memset(tree,0,sizeof(tree));
        LL ans=0;
        for(int i=1;i<=N;i++)
        {
            LL x=getsum(a[i].id);
            ans+=x*((N-a[i].id)-(i-x-1))+(a[i].id-x-1)*(i-x-1);
            add(a[i].id,1);
        }
        cout<<ans<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值