`
Simone_chou
  • 浏览: 183815 次
  • 性别: Icon_minigender_2
  • 来自: 广州
社区版块
存档分类
最新评论

Merry Christmas(二分图最大匹配 + Floyd + 匈牙利算法)

    博客分类:
  • AOJ
 
阅读更多

 

Merry Christmas

Time Limit : 8 sec, Memory Limit : 65536 KB

Problem J: Merry Christmas

International Christmas Present Company (ICPC) is a company to employ Santa and deliver presents on Christmas. Many parents request ICPC to deliver presents to their children at specified time of December 24. Although same Santa can deliver two or more presents, because it takes time to move between houses, two or more Santa might be needed to finish all the requests on time.

Employing Santa needs much money, so the president of ICPC employed you, a great program- mer, to optimize delivery schedule. Your task is to write a program to calculate the minimum number of Santa necessary to finish the given requests on time. Because each Santa has been well trained and can conceal himself in the town, you can put the initial position of each Santa anywhere.

Input

The input consists of several datasets. Each dataset is formatted as follows.

N M L
uvd1
uvd2
.
.
.
uM vM dM
pt1
pt2
.
.
.
pL tL

The first line of a dataset contains three integer, N , M and L (1 ≤ N ≤ 100, 0 ≤ M ≤ 1000, 1 ≤ L ≤ 1000) each indicates the number of houses, roads and requests respectively.

The following M lines describe the road network. The i-th line contains three integers, ui , vi , and di (0 ≤ ui < vi≤ N - 1, 1 ≤ di ≤ 100) which means that there is a road connecting houses ui and vi with di length. Each road is bidirectional. There is at most one road between same pair of houses. Whole network might be disconnected.

The next L lines describe the requests. The i-th line contains two integers, pi and ti (0 ≤ pi ≤ N - 1, 0 ≤ ti ≤ 108 ) which means that there is a delivery request to house pi on time ti . There is at most one request for same place and time. You can assume that time taken other than movement can be neglectable, and every Santa has the same speed, one unit distance per unit time.

The end of the input is indicated by a line containing three zeros separated by a space, and you should not process this as a test case.

Output

Print the minimum number of Santa necessary to finish all the requests on time.

Sample Input

3 2 3
0 1 10
1 2 10
0 0
1 10
2 0
3 2 4
0 1 10
1 2 10
0 0
1 10
2 20
0 40
10 10 10
0 1 39
2 3 48
3 5 20
4 8 43
3 9 10
8 9 40
3 4 5
5 7 20
1 7 93
1 3 20
0 0
1 100000000
2 100
3 543
4 500
5 400
6 300
7 200
8 100
9 100
0 0 0

Output for the Sample Input

2
1
4

 

       题意:

       给出N(1 ~ 100),M(0 ~ 1000),L(1 ~ 1000),代表有 N 个地点,M 个条路,L 个目标。每个地点从0 ~ N-1标号,后给出 M 条路,每条路都是双向边,给出 A,B,V(1 ~ 100),代表A,B之间有一条时间为 V 的路。后给出需要完成的 L 个目标,每个目标给出地点 i 和必须所完成的时间 time(0 ~ 108)。问要如何安排访问次数,使达到所有的目标。

 

       思路:

       二分图最大匹配 + Floyd。分清楚要目的是什么,给出图的信息只是一个已知条件而已,要完成的是目标,如何安排访问路径来使访问次数最少,求最小路径覆盖。首先对图进行 floyd 一次,求出任意两点间的最短路,后每个目标访问点间看能否满足起点时间 + 最短路时间 <= 终点时间 连接起来。后求出最大匹配,再求出最小路径覆盖即可。

 

       AC:

#include <cstdio>
#include <string.h>
#define INF 99999999
using namespace std;

typedef struct {
    int num;
    int time;
}node;
int n,l;
int dis[105][105],w[1005][1005];
int linker[1005],vis[1005];
node no[1005];

void floyd() {
    for(int k = 0;k < n;k++)
        for(int i = 0;i < n;i++)
            for(int j = 0;j < n;j++)
                if(dis[i][k] < INF &&
                   dis[k][j] < INF &&
                   dis[i][j] > dis[i][k] + dis[k][j])
                   dis[i][j] = dis[i][k] + dis[k][j];
}

bool dfs(int u) {
    for(int v = 1;v <= l;v++)
        if(w[u][v] && !vis[v]) {
            vis[v] = 1;
            if(linker[v] == -1 || dfs(linker[v])) {
                linker[v] = u;
                return true;
            }
        }
    return false;
}

int hungary() {
    int res = 0;
    memset(linker,-1,sizeof(linker));
    for(int u = 1;u <= l;u++) {
        memset(vis,0,sizeof(vis));
        if(dfs(u)) res++;
    }
    return res;
}

int main() {
    int m;
    //freopen("test.in","r",stdin);
    while(~scanf("%d%d%d",&n,&m,&l) && (n + m + l)) {
        memset(w,0,sizeof(w));
        for(int i = 0;i < n;i++)
            for(int j = 0;j < n;j++) {
                dis[i][j] = INF;
                if(i == j) dis[i][j] = 0;
            }


        while(m--) {
            int a,b,val;
            scanf("%d%d%d",&a,&b,&val);
            dis[a][b] = val;
            dis[b][a] = val;
        }

        floyd();

        for(int i = 1;i <= l;i++)
            scanf("%d%d",&no[i].num,&no[i].time);

        for(int i = 1;i <= l;i++)
            for(int j = 1;j <= l;j++) {
            if(i == j) continue;
            int f = no[i].num,t = no[j].num;
            int ft = no[i].time,tt = no[j].time;
            if(ft + dis[f][t] <= tt) w[i][j] = 1;
        }

        printf("%d\n",l - hungary());
    }
    return 0;
}

 

 

分享到:
评论

相关推荐

    C++语言+Floyd算法+DFS等等算法实现校园导游咨询系统

    本系统采用Dev C++开发平台来进行编写和测试,用到了类、数组、函数,指针、文件的读取存储操作以及DFS算法和所有顶点对的最短路径(Floyd算法)、 图的各种遍历算法等 用无向网表示XX大学的校园景点平面图,图中顶点...

    MATLAB+Floyd算法程序合集

    利用 MATLAB 实现 Floyd 算法,可对输入的邻接距离矩阵计算图中任意两点间的最短距离矩阵和路由矩阵,且能查询任意两点间的最短距离和路由。 Floyd 算法适用于求解网络中的任意两点间的最短路径:通过图的权值矩阵求...

    Floyd算法 Floyd算法

    中国数学建模-数学工具-Floyd最短路算法的MATLAB程序 wh-ee 重登录 隐身 用户控制面板 搜索 风格 论坛状态 论坛展区 社区服务 社区休闲 网站首页 退出 &gt;&gt; Matlab,Mathematica,maple,几何画板,word,sas,spss......

    基于MATLAB的Floyd算法

    基于MATLAB的Floyd算法,计算网络中任意结点间的最小距离!

    浙大算法包,几何 结构\数论\数值计算\图论_NP搜索\图论_连通性\图论_匹配\组合\

    二分图最大匹配(hungary邻接表形式,邻接阵接口) 二分图最大匹配(hungary正向表形式) 二分图最佳匹配(kuhn_munkras邻接阵形式) 一般图最大匹配(邻接表形式) 一般图最大匹配(邻接阵形式) 一般图最大匹配(正向表...

    Floyd 算法 详细教程

    Floyd算法 详细介绍了Floyd算法的应用

    图:FLoyd算法

    使用Floyd算法,求解点对之间的最短距离。图结构使用邻接矩阵存储。

    floyd弗洛伊德算法

    更新6 floyd弗洛伊德算法更新6 floyd弗洛伊德算法更新6 floyd弗洛伊德算法更新6 floyd弗洛伊德算法更新6 floyd弗洛伊德算法更新6 floyd弗洛伊德算法更新6 floyd弗洛伊德算法更新6 floyd弗洛伊德算法更新6 floyd...

    C# floyd算法 求最短路径

    C# floyd算法 求最短路径 C# floyd算法 求最短路径 C# floyd算法 求最短路径

    matlab匈牙利算法

    这里面有匈牙利算法,floyd算法,Kruskal算法,最佳匹配算法的matlab 程序代码,欢迎下载。。

    Dijkstra(迪杰斯特拉)+Floyd(弗洛伊德)最短路径算法C++实现

    代码直接就能用,比较简单的算法实现

    floyd算法matlab通用程序

    本代码是floyd算法的通用matlab程序,代码写的十分经典,是数模比赛的利器

    ACM算法模板集锦(几何,结构,其他,数论,数值计算,图论)

    二分图最大匹配(hungary邻接表形式,邻接阵接口) 二分图最大匹配(hungary正向表形式) 二分图最佳匹配(kuhn_munkras邻接阵形式) 一般图最大匹配(邻接表形式) 一般图最大匹配(邻接阵形式) 一般图最大匹配(正向表...

    ACM图论数据结构常见模板

    二分图最大权匹配 35 网络流 38 最大流 && 最小割 38 费用流 46 一般数据结构 49 ST Table 49 树状数组 51 树链剖分 52 平衡二叉树 56 Splay 56 数学 64 结论&&推论 64 快速乘法 65 逆元 66 [1, n]素数个数 66 pell...

    ACM算法模板和pku代码

    二分图最大匹配 匈牙利算法 二分图最大匹配 HK算法 二分图最大权匹配 KM算法 割边 强连通分量 缩点 Kosaraju算法 最大团 最小树形图 无向图全局最小割 stoer-wagner O(n^3) 最短路径优先算法 SPFA 网络流 ...

    基于matlab的floyd算法+matlab计算最短路径.doc

    floyd算法+matlab计算,对数学建模爱好者有很大的好处~~~~

    ACM常用模板总结ACM常用模板总结

    二分图最大匹配(hungary邻接表形式,邻接阵接口) 二分图最大匹配(hungary正向表形式) 二分图最佳匹配(kuhn_munkras邻接阵形式) 一般图最大匹配(邻接表形式) 一般图最大匹配(邻接阵形式) 一般图最大匹配(正向表...

    floyd算法的C++代码

    floyd算法的C++代码,用的时候只要改几个参数就可以了

    最短路径算法 floyd

    最短路径算法 floyd

Global site tag (gtag.js) - Google Analytics