Buy or Build

Description
Download as PDF
World Wide Networks (WWN) is a leading company that operates large telecommunication networks. WWN would like to setup a new network in Borduria, a nice country that recently managed to get rid of its military dictator Kurvi-Tasch and which is now seeking for investments of international companies (for a complete description of Borduria, have a look to the following Tintin albums King Ottokar's Sceptre",The Calculus Affair” and “Tintin and the Picaros”). You are requested to help WWN todecide how to setup its network for a minimal total cost.

Problem

There are several local companies running small networks (called subnetworks in the following) that partially cover the n largest cities of Borduria. WWN would like to setup a network that connects all n cities. To achieve this, it can either build edges between cities from scratch or it can buy one or several subnetworks from local companies. You are requested to help WWN to decide how to setup its network for a minimal total cost.
All n cities are located by their two-dimensional Cartesian coordinates.
There are q existing subnetworks. If q 1 then each subnetwork c ( 1 c q ) is defined by a set of interconnected cities (the exact shape of a subnetwork is not relevant to our problem).
A subnetwork c can be bought for a total cost wc and it cannot be split (i.e., the network cannot be fractioned).
To connect two cities that are not connected through the subnetworks bought, WWN has to build an edge whose cost is exactly the square of the Euclidean distance between the cities.
You have to decide which existing networks you buy and which edges you setup so that the total cost is minimal. Note that the number of existing networks is always very small (typically smaller than 8).

A 115 Cities Instance

Consider a 115 cities instance of the problem with 4 subnetworks (the 4 first graphs in Figure 1). As mentioned earlier the exact shape of a subnetwork is not relevant still, to keep figures easy to read, we have assumed an arbitrary tree like structure for each subnetworks. The bottom network in Figure 1 corresponds to the solution in which the first and the third networks have been bought. Thin edges correspond to edges build from scratch while thick edges are those from one of the initial networks.
Input
The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

Each test case is described by one input file that contains all the relevant data. The first line contains the number n of cities in the country ( 1 n 1000 ) followed by the number q of existing subnetworks ( 0 q 8 ). Cities are identified by a unique integer value ranging from 1 to n . The first line is followed by q lines (one per subnetwork), all of them following the same pattern: The first integer is the number of cities in the subnetwork. The second integer is the the cost of the subnetwork (not greater than 2 x 106 ). The remaining integers on the line (as many as the number of cities in the subnetwork) are the identifiers of the cities in the subnetwork. The last part of the file contains n lines that provide the coordinates of the cities (city 1 on the first line, city 2 on the second one, etc). Each line is made of 2 integer values (ranging from 0 to 3000) corresponding to the integer coordinates of the city.
Output
For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

For each input file, your program has to write the optimal total cost to interconnect all cities.
Sample Input
1

7 3
2 4 1 2
3 3 3 6 7
3 9 2 4 5
0 2
4 0
2 0
4 2
1 3
0 5
4 4
Sample Output
17

一些点,点之间的距离平方就是造路的价格,另外还有套餐,套餐中的点是联通的,有固定的花费,套餐可以买多个,问价格最少的生成树。
一开始想到了枚举,但是不知道怎么表示状态,之后学习了状态压缩。

for (i = 0; i < 1 << q; i++)

i就是方案的编号

for (j = 0; j < q; j++)
            {
                if (i >> j & 1)
                {

j是套餐的编号,第j个套餐是否选用。
如果直接枚举会炸,可以不用套餐跑一次,把用到的边存起来,之后枚举套餐的时候,再跑的时候,取边只从这个集合中取,就一定可以组成树。
还有……注意向量清空……

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>
using namespace std;
typedef long long ll;
const int maxn = 1000005;
int  p[1005], mid[10], len, x[1005], y[1005], m, n;
vector<int>cpoint[10];
vector<int>real;
struct Node{
    int u, v;
    int w;
}node[1000000];
bool cmp(Node a,Node b)
{
    return a.w < b.w;
}
int find(int x)
{
    return p[x] == x ? x : p[x] = find(p[x]);
}
int KrusKal()
{
    int ans = 0;
    for (int i = 1; i <= n; i++)
        p[i] = i;
    for (int i = 1; i <= len; i++)
    {
        int x = find(node[i].u);
        int y = find(node[i].v);
        if (x != y)
        {
            ans += node[i].w;
            p[x] = y;
            real.push_back(i);
        }
    }
    return ans;
}
int exKrusKal()
{
    int ans = 0;
    for (int i = 0; i < real.size(); i++)
    {
        int x = find(node[real[i]].u);
        int y = find(node[real[i]].v);
        if (x != y)
        {
            ans += node[real[i]].w;
            p[x] = y;
        }
    }
    return ans;
}
int main()
{
    int i, j, t, q, kase = 1;
    int ans;
    cin >> t;
    while (t--)
    {
        cin >> n >> q;
        for (i = 1; i <= q; i++)
        {
            cpoint[i - 1].clear();
            int tempx, temp1;
            cin >> tempx >> mid[i - 1];
            for (j = 1; j <= tempx; j++)
            {
                cin >> temp1;
                cpoint[i - 1].push_back(temp1);
            }
        }
        real.clear();
        for (i = 1; i <= n; i++)
            cin >> x[i] >> y[i];
        int cnt = 1;
        for (i = 1; i <= n - 1; i++)
        {
            for (j = i + 1; j <= n; j++)
            {
                node[cnt].u = i; node[cnt].v = j;
                node[cnt].w = (x[i] - x[j])*(x[i] - x[j]) + (y[i] - y[j])*(y[i] - y[j]);
                cnt++;
            }
        }
        len = n*(n - 1) / 2;
        sort(node+1, node + 1 + len, cmp);
        ans = KrusKal();
        for (i = 0; i < 1 << q; i++)
        {
            for (j = 1; j <= n; j++)p[j] = j;
            int ans1 = 0;
            for (j = 0; j < q; j++)
            {
                if (i >> j & 1)
                {
                    ans1 += mid[j];
                    for (int k = 1; k < cpoint[j].size(); k++)
                    {
                        int x = find(cpoint[j][0]);
                        int y = find(cpoint[j][k]);
                        if (x != y)
                        {
                            p[y] = x;
                        }
                    }
                }
            }
            ans = min(ans, ans1 + exKrusKal());
        }
        if (kase != 1)cout << endl;
        kase++;
        cout << ans << endl;
    }
    return 0;
}
'iMA' - wrong parameters count 抛物线.mq4 21 17 possible loss of data due to type conversion 抛物线.mq4 21 15 'PositionsTotal' - function not defined 抛物线.mq4 50 13 'PositionsTotal' - function not defined 抛物线.mq4 57 13 'MqlTradeRequest' - undeclared identifier 抛物线.mq4 68 5 'request' - undeclared identifier 抛物线.mq4 68 21 'request' - some operator expected 抛物线.mq4 68 21 'MqlTradeResult' - undeclared identifier 抛物线.mq4 69 5 'result' - undeclared identifier 抛物线.mq4 69 20 'result' - some operator expected 抛物线.mq4 69 20 'action' - struct or class type expected 抛物线.mq4 70 13 'action' - struct or class type expected 抛物线.mq4 70 13 'TRADE_ACTION_DEAL' - undeclared identifier 抛物线.mq4 70 22 'symbol' - struct or class type expected 抛物线.mq4 71 13 'symbol' - struct or class type expected 抛物线.mq4 71 13 implicit conversion from 'string' to 'number' 抛物线.mq4 71 22 'volume' - struct or class type expected 抛物线.mq4 72 13 'volume' - struct or class type expected 抛物线.mq4 72 13 possible loss of data due to type conversion 抛物线.mq4 72 20 'type' - struct or class type expected 抛物线.mq4 73 13 'type' - struct or class type expected 抛物线.mq4 73 13 'price' - struct or class type expected 抛物线.mq4 74 13 'price' - struct or class type expected 抛物线.mq4 74 13 possible loss of data due to type conversion 抛物线.mq4 74 19 'sl' - struct or class type expected 抛物线.mq4 75 13 'sl' - struct or class type expected 抛物线.mq4 75 13 possible loss of data due to type conversion 抛物线.mq4 75 16 'tp' - struct or class type expected 抛物线.mq4 76 13 'tp' - struct or class type expected 抛物线.mq4 76 13 possible loss of data due to type conversion 抛物线.mq4 76 16 'deviation' - struct or class type expected 抛物线.mq4 77 13 'deviation' - struct or class type expected 抛物线.mq4 77 13 'type_filling' - struct or class type expected 抛物线.mq4 78 13 'type_filling' - struct or class type expected 抛物线.mq4 78 13 'ORDER_FILLING_FOK' - undeclared identifier 抛物线.mq4 78 28 'OrderSend' - wrong parameters count 抛物线.mq4 80 10 implicit conversion from 'number' to 'string' 抛物线.mq4 80 20 'MqlTradeRequest' - undeclared identifier 抛物线.mq4 96 5 'request' - undeclared identifier 抛物线.mq4 96 21 'request' - some operator expected 抛物线.mq4 96 21 'MqlTradeResult' - undeclared identifier 抛物线.mq4 97 5 'result' - undeclared identifier 抛物线.mq4 97 20 'result' - some operator expected 抛物线.mq4 97 20 'action' - struct or class type expected 抛物线.mq4 98 13 'action' - struct or class type expected 抛物线.mq4 98 13 'TRADE_ACTION_DEAL' - undeclared identifier 抛物线.mq4 98 22 'symbol' - struct or class type expected 抛物线.mq4 99 13 'symbol' - struct or class type expected 抛物线.mq4 99 13 implicit conversion from 'string' to 'number' 抛物线.mq4 99 22 'volume' - struct or class type expected 抛物线.mq4 100 13 'volume' - struct or class type expected 抛物线.mq4 100 13 possible loss of data due to type conversion 抛物线.mq4 100 20 'type' - struct or class type expected 抛物线.mq4 101 13 'type' - struct or class type expected 抛物线.mq4 101 13 'price' - struct or class type expected 抛物线.mq4 102 13 'price' - struct or class type expected 抛物线.mq4 102 13 possible loss of data due to type conversion 抛物线.mq4 102 19 'sl' - struct or class type expected 抛物线.mq4 103 13 'sl' - struct or class type expected 抛物线.mq4 103 13 possible loss of data due to type conversion 抛物线.mq4 103 16 'tp' - struct or class type expected 抛物线.mq4 104 13 'tp' - struct or class type expected 抛物线.mq4 104 13 possible loss of data due to type conversion 抛物线.mq4 104 16 'deviation' - struct or class type expected 抛物线.mq4 105 13 'deviation' - struct or class type expected 抛物线.mq4 105 13 'type_filling' - struct or class type expected 抛物线.mq4 106 13 'type_filling' - struct or class type expected 抛物线.mq4 106 13 'ORDER_FILLING_FOK' - undeclared identifier 抛物线.mq4 106 28 'OrderSend' - wrong parameters count 抛物线.mq4 108 10 implicit conversion from 'number' to 'string' 抛物线.mq4 108 20 'PositionsTotal' - function not defined 抛物线.mq4 123 18 'PositionGetSymbol' - function not defined 抛物线.mq4 125 13 implicit conversion from 'number' to 'string' 抛物线.mq4 125 13 'POSITION_TYPE' - undeclared identifier 抛物线.mq4 126 32 'PositionGetInteger' - function not defined 抛物线.mq4 126 13 'POSITION_TYPE_BUY' - undeclared identifier 抛物线.mq4 126 50 'MqlTradeRequest' - undeclared identifier 抛物线.mq4 128 13 'request' - undeclared identifier 抛物线.mq4 128 29 'request' - some operator expected 抛物线.mq4 128 29 'MqlTradeResult' - undeclared identifier 抛物线.mq4 129 13 'result' - undeclared identifier 抛物线.mq4 129 28 'result' - some operator expected 抛物线.mq4 129 28 'action' - struct or class type expected 抛物线.mq4 130 21 'action' - struct or class type expected 抛物线.mq4 130 21 'TRADE_ACTION_DEAL' - undeclared identifier 抛物线.mq4 130 30 'symbol' - struct or class type expected 抛物线.mq4 131 21 'symbol' - struct or class type expected 抛物线.mq4 131 21 implicit conversion from 'string' to 'number' 抛物线.mq4 131 30 'volume' - struct or class type expected 抛物线.mq4 132 21 'volume' - struct or class type expected 抛物线.mq4 132 21 'POSITION_VOLUME' - undeclared identifier 抛物线.mq4 132 48 'PositionGetDouble' - function not defined 抛物线.mq4 132 30 'type' - struct or class type expected 抛物线.mq4 133 21 'type' - struct or class type expected 抛物线.mq4 133 21 'position' - struct or class type expected 抛物线.mq4 134 21 'position' - struct or class type expected 抛物线.mq4 134 21 'POSITION_IDENTIFIER' - undeclared identifier 抛物线.mq4 134 51 'PositionGetInteger' - function not defined 抛物线.mq4 134 32 'price' - struct or class type expected 抛物线.mq4 135 21 'price' - struct or class type expected 抛物线.mq4 135 21 possible loss of data due to type conversion 抛物线.mq4 135 27 'deviation' - struct or class type expected 抛物线.mq4 136 21 'deviation' - struct or class type expected 抛物线.mq4 136 21 'type_filling' - struct or class type expected 抛物线.mq4 137 21 'type_filling' - struct or class type expected 抛物线.mq4 137 21 'ORDER_FILLING_FOK' - undeclared identifier 抛物线.mq4 137 36 'OrderSend' - wrong parameters count 抛物线.mq4 139 18 implicit conversion from 'number' to 'string' 抛物线.mq4 139 28 'PositionsTotal' - function not defined 抛物线.mq4 156 18 'PositionGetSymbol' - function not defined 抛物线.mq4 158 13 implicit conversion from 'number' to 'string' 抛物线.mq4 158 13 'POSITION_TYPE' - undeclared identifier 抛物线.mq4 159 32 'PositionGetInteger' - function not defined 抛物线.mq4 159 13 'POSITION_TYPE_SELL' - undeclared identifier 抛物线.mq4 159 50 'MqlTradeRequest' - undeclared identifier 抛物线.mq4 161 13 'request' - undeclared identifier 抛物线.mq4 161 29 'request' - some operator expected 抛物线.mq4 161 29 102 errors, 18 warnings 100 19
07-30
你遇到的错误是由于你的 MT4 平台版本较低,或者你没有使用 **MQL4 的最新标准(Build 600+)**。你的代码使用了较新的 MQL4 语法结构,如: - `MqlTradeRequest` 和 `MqlTradeResult` 结构体 - `PositionsTotal()` 和 `PositionGetInteger()` 等新函数 - `TRADE_ACTION_DEAL`、`ORDER_FILLING_FOK` 等交易常量 这些在旧版本的 MQL4 中是不支持的,因此会报错。 --- ### ✅ 解决方案:使用旧版 MQL4 语法重写代码 下面是一个兼容 **MT4 Build 509 及以下版本** 的完整代码,使用 `OrderSend()`、`OrderClose()` 等旧函数,并移除所有新版结构体和常量。 --- ### ✅ MQL4 旧版代码(兼容 Build 509) ```mql4 //+------------------------------------------------------------------+ //| MA_Strategy.mq4 | //| 自动交易策略:基于均线的多空开平仓逻辑 | //+------------------------------------------------------------------+ #property strict extern int MA_Period = 20; // 均线周期 extern double LotSize = 0.1; // 手数 extern int Slippage = 3; // 滑点 extern int StopLoss = 100; // 止损(点) extern int TakeProfit = 200; // 止盈(点) extern int MagicNumber = 123456; // 订单魔术编号 int ma_handle; //+------------------------------------------------------------------+ //| 初始化函数 | //+------------------------------------------------------------------+ int OnInit() { return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| 主逻辑函数 | //+------------------------------------------------------------------+ void OnTick() { static datetime lastBarTime = 0; datetime currentBarTime = iTime(NULL, 0, 0); // 避免重复处理同一根K线 if (currentBarTime == lastBarTime) return; lastBarTime = currentBarTime; double closePrice = iClose(NULL, 0, 0); double maValue = iMA(NULL, 0, MA_Period, 0, MODE_SMA, PRICE_CLOSE, 0); bool hasBuy = false, hasSell = false; for (int i = 0; i < OrdersTotal(); i++) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) { if (OrderType() == OP_BUY) hasBuy = true; if (OrderType() == OP_SELL) hasSell = true; } } } // 平多仓条件:收盘价跌破均线 if (hasBuy && closePrice < maValue) CloseBuyOrders(); // 平空仓条件:收盘价涨破均线 if (hasSell && closePrice > maValue) CloseSellOrders(); // 做多条件:无持仓,收盘价在均线上方 if (!hasBuy && !hasSell && closePrice > maValue) BuyOrder(); // 做空条件:无持仓,收盘价在均线下方 if (!hasBuy && !hasSell && closePrice < maValue) SellOrder(); } //+------------------------------------------------------------------+ //| 买入开仓函数 | //+------------------------------------------------------------------+ void BuyOrder() { double price = Ask; int ticket = OrderSend(Symbol(), OP_BUY, LotSize, price, Slippage, price - StopLoss * Point, price + TakeProfit * Point, "Buy Order", MagicNumber, 0, clrGreen); if (ticket < 0) Print("买入订单失败: ", GetLastError()); else Print("买入订单已提交"); } //+------------------------------------------------------------------+ //| 卖出开仓函数 | //+------------------------------------------------------------------+ void SellOrder() { double price = Bid; int ticket = OrderSend(Symbol(), OP_SELL, LotSize, price, Slippage, price + StopLoss * Point, price - TakeProfit * Point, "Sell Order", MagicNumber, 0, clrRed); if (ticket < 0) Print("卖出订单失败: ", GetLastError()); else Print("卖出订单已提交"); } //+------------------------------------------------------------------+ //| 平多仓函数 | //+------------------------------------------------------------------+ void CloseBuyOrders() { for (int i = 0; i < OrdersTotal(); i++) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == OP_BUY) { if (!OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, clrRed)) Print("平多仓失败: ", GetLastError()); else Print("多单已平仓"); } } } } //+------------------------------------------------------------------+ //| 平空仓函数 | //+------------------------------------------------------------------+ void CloseSellOrders() { for (int i = 0; i < OrdersTotal(); i++) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == OP_SELL) { if (!OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, clrGreen)) Print("平空仓失败: ", GetLastError()); else Print("空单已平仓"); } } } } ``` --- ### ✅ 关键点说明: | 旧版函数/变量 | 新版函数/变量 | 说明 | |--------------|----------------|------| | `OrderSend()` | `OrderSend(request, result)` | 旧版直接传参,新版使用结构体 | | `OrderClose()` | `OrderSend(...TRADE_ACTION_DEAL)` | 旧版直接平仓,新版使用交易请求 | | `OrdersTotal()` | `PositionsTotal()` | 旧版使用订单循环,新版使用持仓循环 | | `OP_BUY/OP_SELL` | `ORDER_TYPE_BUY/SELL` | 交易类型常量不同 | | `Point` | `_Point` | 价格最小变动单位 | --- ### ✅ 使用说明: 1. 打开 MetaTrader 4,进入 MetaEditor。 2. 创建一个新的 Expert Advisor。 3. 替换模板代码为上面的代码。 4. 编译并加载到图表上即可运行。 5. 可通过右键图表 -> “交易” -> 启用自动交易。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值