UVA 10746 Crime Wave - The Sequel

这个题目真是无力吐槽,WA了几次之后去uva论坛一看原来是精度问题,对比了别人的测试数据发现因为某一个输出 总是比别人的小0.01,最短路判断改成这样:if(p->c && dis[j = p->i] > dis[i] + p->d + esp)还是无果, 然后将dis[s]初始化为esp,这样就才过掉。以后做这样的类型的题目初始值都为esp好了。

思路:

n个银行,m个警察,m个警察到每个银行的时间给定,最后求每个银行派遣一个警察,要求到达总时间最小,输出每个银行的平均时间。

每个警察建立到每一个银行的边,容量1,费用就是相应到达时间,然后源点到每个警察一条边,容量1,费用0;每个银行到汇点同样一条边,容量1,费用0,spfa求之。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#define esp 1e-6
#define in  freopen("in.txt", "r", stdin);
#define out freopen("out.txt", "w", stdout);
#define print(a) printf("%d\n",(a));
#define bug puts("********))))))");
#define inf 0x0f0f0f0f
#define INF 0x0f0f0f0f0f0f
#define N 100000
#define M 10000000
using namespace std;
struct EDGE
{
    int i, c;
    double d;
    EDGE *next, *ani;
} *Edge[N], *Path[N], E[M];
double dis[N],cost;
int n, m, cnt;
int f, inq[N];
int src, sink;
int dblcmp(double x)
{
    if(fabs(x) < esp)
        return 0;
    return x > 0 ? 1 : -1;
}
void init(void)
{
    memset(Edge, 0, sizeof(Edge));
    cnt = 0;
}
void add(int i, int j, int c, double d, EDGE &e1, EDGE &e2)
{
    e1.i = j, e1.c = c, e1.d = d, e1.next = Edge[i], e1.ani = &e2, Edge[i] = &e1;
    e2.i = i, e2.c = 0, e2.d = -d, e2.next = Edge[j], e2.ani = &e1, Edge[j] = &e2;
}
bool SPFA(int s, int end)
{
    memset(Path, 0, sizeof(Path));//每次spfa开始都将路径初始化
    memset(inq, 0, sizeof(inq));
    for(int i = 0; i < N; i++)
        dis[i] = INF;
    inq[s] = 1;
    dis[s] = esp;//初始化为esp
    queue<int> q;
    q.push(s);
    while(!q.empty())
    {
        int i, j;
        i = q.front();
        q.pop();
        inq[i] = 0;
        for(EDGE *p = Edge[i]; p; p = p->next)
            if(p->c && dis[j = p->i] > dis[i] + p->d + esp)
                if(Path[j] = p, dis[j] = dis[i] + p->d,!inq[j])
                    inq[j] = 1, q.push(j);
    }
    return dis[end] < INF;
}
double  min_cost_flow(int s,int end)
{
    int flow = inf;
    f = cost = 0;
    while(SPFA(s, end))
    {
        EDGE *p;
        for(p = Path[end]; p; p = Path[p->ani->i])
            flow = min(flow, p->c);
        for(p = Path[end], f += flow, cost += 1.0 * flow * dis[end];
                p; p = Path[p->ani->i])
            p->c -= flow,p->ani->c += flow;
    }
    return cost;
}
int main(void)
{in
    while(scanf("%d%d", &n, &m),n)
    {
        init();
        src = 0;
        sink = n+m+1;
        int flag = 0;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= m; j++)
            {
                double w;
                scanf("%lf", &w);
                add(j+n, i, 1, w, E[cnt], E[cnt + 1]);
                cnt += 2;
                if(!flag)
                    add(src, j+n, 1, 0.0, E[cnt], E[cnt + 1]),
                        cnt += 2;
            }
            if(flag == 0)
                flag = 1;
            add(i, sink, 1, 0.0, E[cnt], E[cnt + 1]);
            cnt += 2;
        }
        double ans = min_cost_flow(src, sink);
        ans /= 1.0*n;
//        print(f)
       printf("%.2f\n",ans);
    }
    return 0;
}
附上一组数据:

input:



3 14
42.4 24.8 39.1 85.5 81.5 7.4 3.4 62.5 40.3 87.0 73.2 31.9 87.5 14.7
72.8 5.3 36.1 91.7 54.0 60.2 4.9 69.0 3.0 14.8 20.8 70.9 10.4 60.3
42.5 77.7 86.3 20.0 37.6 60.5 40.6 19.1 67.8 79.1 16.7 43.2 66.0 25.1
3 11
74.9 83.0 93.9 11.0 9.9 47.8 6.3 14.7 51.9 44.5 64.6
7.9 15.3 74.9 68.1 92.9 87.8 89.5 12.8 25.3 50.0 88.5
79.5 52.9 67.5 31.4 96.1 68.6 56.4 6.3 57.2 66.4 24.4
3 10
34.2 33.9 83.6 84.1 85.8 28.0 48.6 93.6 78.4 58.7
61.6 71.2 46.4 86.2 19.1 6.8 36.1 42.7 86.3 89.0
10.1 52.8 20.2 78.6 9.1 26.4 70.9 10.6 50.7 21.8
6 19
90.9 6.6 4.1 76.6 34.5 52.6 5.3 48.0 46.4 2.0 54.3 92.7 88.1 73.3 34.7 24.2 51.1 20.9 48.3
61.1 73.6 68.4 74.8 17.8 29.9 45.7 28.3 80.5 2.6 51.3 0.5 93.4 57.8 4.5 5.1 27.4 92.3 10.3
75.3 38.6 47.4 29.5 66.5 35.5 37.9 1.1 94.8 88.9 57.1 78.2 85.1 65.8 46.5 59.9 18.7 11.5 5.5
82.1 91.9 43.2 33.4 27.6 36.6 91.1 32.0 41.6 18.5 59.4 87.1 28.9 33.2 34.4 93.6 99.6 5.0 31.4
35.8 34.9 55.5 92.8 13.0 40.5 93.7 94.6 35.5 12.3 6.0 76.1 94.3 33.1 19.3 27.6 60.6 91.0 53.9
27.7 67.7 7.5 87.1 54.7 36.3 55.4 24.3 29.8 54.9 64.4 96.4 25.8 34.5 87.0 53.7 82.6 62.6 47.3
1 4
94.7 53.5 9.3 88.9
2 5
51.7 82.2 54.6 5.5 9.9
22.3 48.1 32.1 12.1 84.3
11 14
49.3 77.4 71.1 80.8 3.1 5.5 67.7 56.7 88.0 30.2 39.1 35.5 63.4 33.7
89.0 72.7 22.5 75.4 36.3 9.3 57.6 26.1 14.7 2.6 48.3 62.7 69.8 95.5
82.2 57.1 2.2 31.4 69.6 73.2 12.1 72.6 13.8 14.9 64.4 36.9 80.2 3.4
72.4 43.6 72.2 61.3 51.4 94.6 71.8 22.8 3.9 64.5 48.8 53.7 67.0 32.2
51.6 36.7 27.7 33.7 28.9 65.0 0.2 33.6 38.1 47.4 41.3 51.8 97.4 5.6
88.6 77.5 44.1 60.9 21.0 16.2 22.1 72.3 46.0 29.1 30.3 85.0 93.5 79.0
73.8 60.5 11.2 25.3 32.3 74.0 94.1 96.4 38.9 29.4 29.9 76.9 76.7 6.4
63.8 9.2 11.9 52.3 86.7 91.2 48.4 7.6 42.5 5.6 15.1 88.4 34.6 45.3
8.5 28.1 59.4 82.3 23.7 70.5 42.7 91.1 79.6 36.8 87.4 18.4 66.1 52.5
30.4 78.0 58.8 29.3 87.1 5.8 16.8 8.9 96.9 65.1 16.5 39.4 5.8 66.7
62.9 40.4 47.1 71.4 3.6 6.4 88.8 27.2 12.1 31.4 18.2 26.8 3.3 40.8
2 12
28.4 10.7 82.5 87.1 40.0 4.7 92.8 56.7 13.6 24.9 56.9 65.2
99.4 62.6 67.0 97.4 38.1 14.0 3.9 76.8 55.5 92.6 39.1 2.7
9 12
29.5 62.4 33.2 9.8 31.7 61.5 20.4 49.3 83.7 95.5 89.2 11.6
52.1 37.9 71.6 44.1 3.0 70.9 41.9 69.9 68.3 79.9 19.0 7.3
91.9 9.6 99.9 30.9 12.3 59.0 23.3 41.7 21.3 91.6 51.4 88.2
88.2 6.9 72.6 71.8 2.4 61.7 83.4 89.6 99.5 54.9 68.9 37.6
61.0 10.7 42.6 64.4 90.5 61.5 71.6 17.5 6.3 6.6 48.4 18.5
65.5 71.6 95.3 22.0 63.2 81.8 45.3 51.3 88.6 17.8 58.3 26.1
14.7 76.8 15.7 49.3 66.8 84.5 86.9 27.7 30.3 29.4 27.2 55.9
26.1 98.8 73.4 32.3 5.3 21.7 85.9 6.0 28.4 81.1 27.9 91.5
62.8 73.1 78.0 86.5 26.0 71.4 12.6 40.6 48.1 63.4 89.9 50.0
9 10
77.7 13.2 41.2 4.8 69.0 2.4 38.7 42.3 69.8 44.0
63.9 55.6 49.9 92.3 71.8 12.9 18.9 69.7 21.1 32.0
56.2 47.0 38.5 3.9 22.8 86.5 67.2 47.8 71.7 85.3
59.6 49.3 98.4 35.9 89.2 67.3 73.5 27.9 44.8 43.2
7.0 43.8 34.0 92.0 71.2 5.7 4.8 25.3 10.6 61.0
57.2 66.7 43.1 30.9 5.7 65.8 52.5 8.0 48.7 24.1
93.2 43.4 73.3 26.7 79.3 97.7 29.1 87.9 60.7 73.8
31.0 67.6 17.6 0.1 94.7 23.9 41.0 99.4 49.1 51.5
95.5 41.5 53.3 38.5 72.3 58.9 39.5 24.7 66.8 88.1
7 8
66.7 57.2 21.7 81.1 90.0 50.7 68.9 50.6
59.7 35.0 53.3 12.4 35.1 47.9 36.2 76.0
82.4 85.3 27.4 77.8 26.7 80.6 16.3 98.9
74.6 90.9 58.7 76.5 14.1 7.4 71.5 80.7
99.8 28.3 61.7 89.7 14.1 30.5 40.3 73.7
65.5 28.7 86.0 35.7 76.6 22.2 11.6 58.9
7.4 74.1 36.7 69.2 89.8 88.1 68.0 64.3
2 9
75.9 28.1 4.4 47.3 8.8 4.1 10.7 5.6 28.9
24.7 71.3 4.3 98.4 36.7 33.0 19.5 7.5 9.5
14 16
68.3 84.1 28.2 4.9 88.4 53.1 28.1 56.3 17.3 42.1 53.3 28.3 70.2 57.6 10.7 14.1
61.6 21.3 19.6 90.4 45.9 90.8 29.9 79.4 62.6 62.8 98.9 5.2 72.2 75.6 59.3 40.4
94.9 87.4 80.5 83.2 40.4 43.7 74.7 92.8 85.8 27.9 21.0 91.1 85.4 66.8 5.1 82.1
88.0 59.8 7.6 69.1 85.8 37.4 83.6 83.5 0.1 17.6 88.7 7.4 93.2 47.9 83.0 88.0
70.5 63.4 6.3 10.8 42.2 80.9 38.8 27.9 8.7 94.9 54.1 29.2 61.7 59.1 11.2 84.8
54.1 54.0 89.0 75.0 91.3 72.6 93.6 26.6 90.1 82.2 33.9 18.4 65.3 16.8 41.5 35.7
15.3 47.8 81.6 57.5 28.6 55.5 85.3 72.5 50.4 74.6 1.6 47.2 68.8 48.0 31.9 22.8
1.9 56.1 32.9 28.3 28.6 26.5 54.8 53.8 43.8 23.9 72.2 9.0 40.6 13.6 79.8 55.9
61.3 61.4 48.5 25.1 16.8 68.9 97.5 2.3 43.4 34.2 84.6 12.2 82.1

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值