(floyd+匈牙利算法) poj 3216

O - Repairing Company

Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u

Submit Status Practice POJ 3216

Description

Lily runs a repairing company that services the Q blocks in the city. One day the company receives M repair tasks, the ith of which occurs in block pi, has a deadline ti on any repairman’s arrival, which is also its starting time, and takes a single repairman di time to finish. Repairmen work alone on all tasks and must finish one task before moving on to another. With a map of the city in hand, Lily want to know the minimum number of repairmen that have to be assign to this day’s tasks.

Input

The input contains multiple test cases. Each test case begins with a line containing Q and M (0 < Q ≤ 20, 0 < M ≤ 200). Then follow Q lines each with Q integers, which represent a Q × Q matrix Δ = {δij}, where δij means a bidirectional road connects the ith and the jth blocks and requires δijtime to go from one end to another. If δij = −1, such a road does not exist. The matrix is symmetric and all its diagonal elements are zeroes. Right below the matrix are M lines describing the repairing tasks. The ith of these lines contains piti and di. Two zeroes on a separate line come after the last test case.

Output

For each test case output one line containing the minimum number of repairmen that have to be assigned.

Sample Input

1 2
0
1 1 10
1 5 10
0 0

Sample Output

2

题意:

给出Q的街道和M个任务

然后给出i*j的矩阵..表示第i个街道到第j个街道的距离 其中-1表示不可到达

然后接下来M行有 p t d 表示 任务在p街道 开始时间是t 完成工作花费时间是d

问最少派出多少人可以完成M个任务

思路:

用floyd求出街道之间的最短距离

根据两个任务距离花费时间+完成工作花费时间+工作开始时间<=第二个工作开始时间

确定两个任务是否可以由一个人完成..

然后得到一个二分图..

然后用n-最大匹配 求出最短路径匹配

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
#define INF 100000000
int q,m;
int a[25][25],mp[205][205],link[205],mark[205];
struct node
{
    int p,t,d;
}job[205];
bool dfs(int x)
{
    for(int i=1;i<=m;i++)
    {
        if(mark[i]==-1&&mp[x][i])
        {
            mark[i]=1;
            if(link[i]==-1||dfs(link[i]))
            {
                link[i]=x;
                return true;
            }
        }
    }
    return false;
}
void floyd()
{
    for(int k=1;k<=q;k++)
    {
        for(int i=1;i<=q;i++)
        {
            for(int j=1;j<=q;j++)
            {
                if(a[i][j]>a[i][k]+a[k][j])
                    a[i][j]=a[i][k]+a[k][j];
            }
        }
    }
}
int main()
{
    while(scanf("%d%d",&q,&m)!=EOF)
    {
        if(q==0&&m==0)
            break;
        int ans=0;
        memset(mp,0,sizeof(mp));
        memset(link,-1,sizeof(link));
        for(int i=1;i<=q;i++)
        {
            for(int j=1;j<=q;j++)
            {
                scanf("%d",&a[i][j]);
                if(a[i][j]==-1)
                    a[i][j]=INF;
            }
        }
        floyd();
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&job[i].p,&job[i].t,&job[i].d);
        }
        for(int i=1;i<=m;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(i!=j)
                {
                    int x=job[i].p;
                    int y=job[j].p;
                    if(job[i].t+job[i].d+a[x][y]<=job[j].t)
                        mp[i][j]=1;
                }
            }
        }
        for(int i=1;i<=m;i++)
        {
            memset(mark,-1,sizeof(mark));
            if(dfs(i))
                ans++;
        }
        printf("%d\n",m-ans);
    }
    return 0;
}

  

				
时间: 2024-10-04 19:18:01

(floyd+匈牙利算法) poj 3216的相关文章

(floyd+匈牙利算法) poj 2594

P - Treasure Exploration Time Limit:6000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2594 Description Have you ever read any book about treasure exploration? Have you ever see any film about treasure explor

二分图匹配(匈牙利算法) POJ 3020 Antenna Placement

题目传送门 1 /* 2 题意:*的点占据后能顺带占据四个方向的一个*,问最少要占据多少个 3 匈牙利算法:按坐标奇偶性把*分为两个集合,那么除了匹配的其中一方是顺带占据外,其他都要占据 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cstring> 8 #include <vector> 9 using namespace std; 10 11 const int MAXN = 4e

二分图匹配(匈牙利算法) POJ 3041 Asteroids

题目传送门 1 /* 2 题意:每次能消灭一行或一列的障碍物,要求最少的次数. 3 匈牙利算法:把行和列看做两个集合,当有障碍物连接时连一条边,问题转换为最小点覆盖数==二分图最大匹配数 4 趣味入门:http://blog.csdn.net/dark_scope/article/details/8880547 5 */ 6 #include <cstdio> 7 #include <algorithm> 8 #include <cstring> 9 #include

POJ 3041 Asteroids (匈牙利算法)

Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14388 Accepted: 7828 Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <= 500). The grid contains K astero

POJ 3014:Asteroids(二分匹配,匈牙利算法)

Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14399   Accepted: 7836 Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <= 500). The grid contains K as

POJ 3020 Antenna Placement(二分图 匈牙利算法)

题目网址:  http://poj.org/problem?id=3020 题意: 用椭圆形去覆盖给出所有环(即图上的小圆点),有两种类型的椭圆形,左右朝向和上下朝向的,一个椭圆形最多可以覆盖相邻的两个小圆点.   思路: 将每个小圆点看作是一个顶点,因为一个椭圆只能覆盖两个小圆点,我们就可以把这个图看成一个二分图.将相邻的两个点,一个看作是X集合内顶点,另一个看成是Y集合内顶点.但要注意的是一个顶点可能不止和一个顶点想连(如上图情况),所以我们要把上述情况看作是一个无向图,而不是有向图.无向图

POJ 1325 Machine Schedule (二分图最小点集覆盖 匈牙利算法)

Machine Schedule Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12621   Accepted: 5399 Description As we all know, machine scheduling is a very classical problem in computer science and has been studied for a very long history. Scheduli

poj - 3041 Asteroids (二分图最大匹配+匈牙利算法)

http://poj.org/problem?id=3041 在n*n的网格中有K颗小行星,小行星i的位置是(Ri,Ci),现在有一个强有力的武器能够用一发光速将一整行或一整列的小行星轰为灰烬,想要利用这个武器摧毁所有的小行星最少需要几发光束. 主要是构图,将每一行当成一个点,构成集合1,每一列也当成一个点,构成集合2,每一个障碍物的位置坐标将集合1和集合2的点连接起来,也就是将每一个障碍物作为连接节点的边,这样可以得出本题是一个最小点覆盖的问题==二分图的最大匹配. 就可以通过匈牙利算法求解.

POJ 1325 二分图匹配/匈牙利算法

Machine Schedule Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11922 Accepted: 5077 Description As we all know, machine scheduling is a very classical problem in computer science and has been studied for a very long history. Scheduling p