(floyd+ Hungarian algorithm) POJ 3216

Source: Internet
Author: User

O-repairing CompanyTime limit:1000MS Memory Limit:131072KB 64bit IO Format:%i64d &%i6 4u SubmitStatusPracticePOJ 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, have a Deadlin E ti on any repairman ' s arrival, which are also its starting time, and takes a single repairman di time t o 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 has to being assign to this day's Tas Ks.

Input

The input contains multiple test cases. Each test case is 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 x q matrixδ= {Δij< /c8>}, whereδij means a bidirectional road connects the ith and the jth blocks and require Sδijtime to go from the one end to another. Ifδij =−1, such a road does not exist. The matrix is symmetric and all their diagonal elements are zeroes. Right below the matrix is M lines describing the repairing tasks. The ith of these lines contains pi, ti and di. The last test case, zeroes on a separate line come.

Output

The For all test case is output one line containing the minimum number of repairmen, that has to be assigned.

Sample Input

1 201 1 101 5 100 0

Sample Output

2

Test instructions

Give the streets and m quests of Q

Then give the i*j matrix. Represents the distance from the first street to the J Street where-1 means unreachable

Then the next m line has P T D to indicate that the task at P Street start time is T completed work takes time to be D

Ask at least how many people can complete m missions

Ideas:

Use Floyd to find the shortest distance between streets

Spend time on two task distances + time to complete work + work start time <= second start of work

Determine whether two tasks can be completed by a single person.

And then get a two-point diagram.

Then we find the shortest path matching with n-Max match

#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 100000000int 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]) {MA            Rk[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", &AMP;JOB[I].P,&AMP;JOB[I].T,&AMP;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;}

  



(floyd+ Hungarian algorithm) POJ 3216

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.