Computer Assembling //2010 ACM-ICPC Multi-University Training Contest(10)——Host by HEU

Computer Assembling

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 380    Accepted Submission(s): 166

Problem Description
XiaoA is becoming more and more unsatisfied with his computer since he is learning hacker technologies these days but his computer always fails whenever he changes the configurations of the NIC. He buys a new NIC but the motherboard doesn’t recognize it. He then pays for a motherboard from the same company as the NIC. This time the display card is in collision with the motherboard so he cannot even start his computer.
After days of affliction, XiaoA decides to assemble a computer himself. He has to buy n components. Two companies are finally chosen after he searches the web. They both offer all the n components but the prices may vary. There are some pairs of components such that if XiaoA buys them from different companies he has to spend an additional fee to buy adapters to avoid the collision problem.
XiaoA wants to spend as little as possible. Please tell him the minimum dollars he has to pay.
 

 

Input
There are multiple test cases.
For each test case, the first line contains two integers n and m(1<=n<=500, 0<=m<=n*(n-1)/2), indicating the number of components and the number of pairs. The components are conveniently numbered from 1 to n and all pairs are distinct. The next two lines both have n integers(in the range [1, 200]), describing the price in dollar for each component they offer in order. The next m lines contain three integers i, j and c(1<=i, j<=n, i!=j, 1<=c<=200) each, indicating that if he buys component i and j from different companies, he has to spend another c dollars for an adapter.
Proceed to the end of file.
 

 

Output
For each test case, print the minimum cost on a single line.
 

 

Sample Input
  
  
5 3 2 3 4 5 2 1 5 3 6 4 1 2 3 2 3 5 3 4 2
 

 

Sample Output
  
  
16
 

 

Source
 

 

Recommend
zhengfeng
 

 

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,k,flow[510][510],dist[510],gap[510];//注意范围
int find_path(int p, int limit = 0x3f3f3f3f)
{
    if (p == n - 1) return limit;
    for (int i = 0; i < n; ++i)
    {
        if (dist[p] == dist[i] + 1 && flow[p][i] > 0)
        {
            int t = find_path(i, min(limit, flow[p][i]));
            if (t < 0) return t;
            if (t > 0)
            {
                flow[p][i] -= t;
                flow[i][p] += t;
                return t;
            }
        }
    }
    int label = n;
    for (int i = 0; i < n; ++i) if (flow[p][i] >0)  label = min(label, dist[i] + 1);
    if (--gap[dist[p]] == 0 || dist[0] >= n) return -1;
    ++gap[dist[p] = label];
    return 0;
}
int iSAP()
{
    gap[0] = n;
    int maxflow = 0,t = 0;
    while ((t = find_path(0)) >= 0)  maxflow += t;
    return maxflow;
}
int main()
{
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        memset(flow,0,sizeof(flow));
        memset(gap,0,sizeof(gap));
        memset(dist,0,sizeof(dist));//注意初始化
        for(int i=1;i<=n;++i)
        {
           int x;
           scanf("%d", &x);
           flow[0][i] += x;//注意是无向图还是有向图
           //flow[i][0] += x;
        }
        for(int i=1;i<=n;++i)
        {
            int x;
            scanf("%d",&x);
            //flow[2*n+1][i]+=x;
            flow[i][n+1]+=x;
        }
        for(int i=0;i<k;i++) //题目特殊要求,不在模板中
        {
            int x,y,c;
            scanf("%d%d%d",&x,&y,&c);
            flow[x][y]+=c;
            flow[y][x]+=c;
        }                //注意这个模板是从0开始的,源点放0,汇点放N+1处
        n=n+2;//注意要加1,因为增加了一个汇点
        printf("%d/n", iSAP());
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值