POJ2060最小路径覆盖
生活随笔
收集整理的這篇文章主要介紹了
POJ2060最小路径覆盖
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題意:
? ? ? 有n個任務,如果時間來得及干完某些任務后還可以接著干別的任務,給一個任務清單,問最少派出去多少人能完成所有任務。
思路:?
? ? ? 比較簡單的追小路徑覆蓋問題了,在DAG中找到最少的簡單路徑去覆蓋所有點,結論等于n-最大匹配數,可以這樣理解,最開始沒有邊任務都需要一個人,共n個,然后只要有一條邊(干完A活來的及干B活那么連邊AB),就有可能減少一個人,當A-B A-C這樣的時候只能節省其中的一條,匹配也是,只能把A匹配給一個,這樣說是不是很容易理解為什么最小路徑覆蓋的結論是n-最大匹配數了吧。
#include<stdio.h>
#include<string.h>
#define N_node 500 + 10
#define N_edge 500 * 500 + 100
typedef struct
{
? ? int time ,t,x1 ,x2 ,y1 ,y2;
}NODE;
typedef struct
{
? ? int to ,next;
}STAR;
NODE node[N_node];
STAR E[N_edge];
int list[N_node] ,tot;
int mkgx[N_node] ,mkdfs[N_node];
void add(int a ,int b)
{
? ? E[++tot].to = b;
? ? E[tot].next = list[a];
? ? list[a] = tot;
}
int DFS_XYL(int x)
{
? ? for(int k = list[x] ;k ;k = E[k].next)
? ? {
? ? ? ? int to = E[k].to;
? ? ? ? if(mkdfs[to]) continue;
? ? ? ? mkdfs[to] = 1;
? ? ? ? if(mkgx[to] == -1 || DFS_XYL(mkgx[to]))
? ? ? ? {
? ? ? ? ? ? mkgx[to] = x;
? ? ? ? ? ? return 1;
? ? ? ? }
? ? }
? ? return 0;
}
int abss(int x)
{
? ? return x < 0 ? -x : x;
}
bool ok(int a ,int b)
{
? ? int t1 = abss(node[a].x1 - node[a].x2) + abss(node[a].y1 - node[a].y2);
? ? int t2 = abss(node[a].x2 - node[b].x1) + abss(node[a].y2 - node[b].y1);
? ? return node[b].t - node[a].t > t1 + t2;
}
int main ()
{
? ? int t ,n ,i ,j ,a ,b;
? ? scanf("%d" ,&t);
? ? while(t--)
? ? {
? ? ? ? scanf("%d" ,&n);
? ? ? ? int tmp = 0;
? ? ? ? for(i = 1 ;i <= n ;i ++)
? ? ? ? {
? ? ? ? ? ? scanf("%d:%d %d %d %d %d" ,&a ,&b ,&node[i].x1 ,&node[i].y1 ,&node[i].x2 ,&node[i].y2);
? ? ? ? ? ? node[i].time = a * 60 + b;
? ? ? ? ? ? if(i != 1 && node[i].time < node[i-1].time)
? ? ? ? ? ? tmp ++;
? ? ? ? ? ? node[i].t = node[i].time + tmp * 24 * 60;
? ? ? ? }
? ? ? ? memset(list ,0 ,sizeof(list));
? ? ? ? tot = 1;
? ? ? ? for(i = 1 ;i <= n ;i ++)
? ? ? ? for(j = i + 1 ;j <= n ;j ++)
? ? ? ? {
? ? ? ? ? ? if(ok(i ,j)) add(i ,j);
? ? ? ? }
? ? ? ? memset(mkgx ,255 ,sizeof(mkgx));
? ? ? ? int Ans = 0;
? ? ? ? for(i = 1 ;i <= n ;i ++)
? ? ? ? {
? ? ? ? ? ? memset(mkdfs ,0 ,sizeof(mkdfs));
? ? ? ? ? ? Ans += DFS_XYL(i);
? ? ? ? }
? ? ? ? printf("%d\n" ,n - Ans);
? ? }
? ? return 0;
}
? ? ? 有n個任務,如果時間來得及干完某些任務后還可以接著干別的任務,給一個任務清單,問最少派出去多少人能完成所有任務。
思路:?
? ? ? 比較簡單的追小路徑覆蓋問題了,在DAG中找到最少的簡單路徑去覆蓋所有點,結論等于n-最大匹配數,可以這樣理解,最開始沒有邊任務都需要一個人,共n個,然后只要有一條邊(干完A活來的及干B活那么連邊AB),就有可能減少一個人,當A-B A-C這樣的時候只能節省其中的一條,匹配也是,只能把A匹配給一個,這樣說是不是很容易理解為什么最小路徑覆蓋的結論是n-最大匹配數了吧。
#include<stdio.h>
#include<string.h>
#define N_node 500 + 10
#define N_edge 500 * 500 + 100
typedef struct
{
? ? int time ,t,x1 ,x2 ,y1 ,y2;
}NODE;
typedef struct
{
? ? int to ,next;
}STAR;
NODE node[N_node];
STAR E[N_edge];
int list[N_node] ,tot;
int mkgx[N_node] ,mkdfs[N_node];
void add(int a ,int b)
{
? ? E[++tot].to = b;
? ? E[tot].next = list[a];
? ? list[a] = tot;
}
int DFS_XYL(int x)
{
? ? for(int k = list[x] ;k ;k = E[k].next)
? ? {
? ? ? ? int to = E[k].to;
? ? ? ? if(mkdfs[to]) continue;
? ? ? ? mkdfs[to] = 1;
? ? ? ? if(mkgx[to] == -1 || DFS_XYL(mkgx[to]))
? ? ? ? {
? ? ? ? ? ? mkgx[to] = x;
? ? ? ? ? ? return 1;
? ? ? ? }
? ? }
? ? return 0;
}
int abss(int x)
{
? ? return x < 0 ? -x : x;
}
bool ok(int a ,int b)
{
? ? int t1 = abss(node[a].x1 - node[a].x2) + abss(node[a].y1 - node[a].y2);
? ? int t2 = abss(node[a].x2 - node[b].x1) + abss(node[a].y2 - node[b].y1);
? ? return node[b].t - node[a].t > t1 + t2;
}
int main ()
{
? ? int t ,n ,i ,j ,a ,b;
? ? scanf("%d" ,&t);
? ? while(t--)
? ? {
? ? ? ? scanf("%d" ,&n);
? ? ? ? int tmp = 0;
? ? ? ? for(i = 1 ;i <= n ;i ++)
? ? ? ? {
? ? ? ? ? ? scanf("%d:%d %d %d %d %d" ,&a ,&b ,&node[i].x1 ,&node[i].y1 ,&node[i].x2 ,&node[i].y2);
? ? ? ? ? ? node[i].time = a * 60 + b;
? ? ? ? ? ? if(i != 1 && node[i].time < node[i-1].time)
? ? ? ? ? ? tmp ++;
? ? ? ? ? ? node[i].t = node[i].time + tmp * 24 * 60;
? ? ? ? }
? ? ? ? memset(list ,0 ,sizeof(list));
? ? ? ? tot = 1;
? ? ? ? for(i = 1 ;i <= n ;i ++)
? ? ? ? for(j = i + 1 ;j <= n ;j ++)
? ? ? ? {
? ? ? ? ? ? if(ok(i ,j)) add(i ,j);
? ? ? ? }
? ? ? ? memset(mkgx ,255 ,sizeof(mkgx));
? ? ? ? int Ans = 0;
? ? ? ? for(i = 1 ;i <= n ;i ++)
? ? ? ? {
? ? ? ? ? ? memset(mkdfs ,0 ,sizeof(mkdfs));
? ? ? ? ? ? Ans += DFS_XYL(i);
? ? ? ? }
? ? ? ? printf("%d\n" ,n - Ans);
? ? }
? ? return 0;
}
總結
以上是生活随笔為你收集整理的POJ2060最小路径覆盖的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: POJ2044 深搜+剪枝(云彩下雨)
- 下一篇: POJ2195费用流+BFS建图