Qin Shi Huang's National Road System

秦始皇之路网优化
本文探讨了一个关于古代中国秦始皇时期道路系统的数学问题,通过寻找最优的次小生成树来最小化非魔法道路的总长度,并最大化受益人口与成本的比例。

次小生成树优先队列优化

 

Qin Shi Huang's National Road System

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5613    Accepted Submission(s): 1975


Problem Description
During the Warring States Period of ancient China(476 BC to 221 BC), there were seven kingdoms in China ---- they were Qi, Chu, Yan, Han, Zhao, Wei and Qin. Ying Zheng was the king of the kingdom Qin. Through 9 years of wars, he finally conquered all six other kingdoms and became the first emperor of a unified China in 221 BC. That was Qin dynasty ---- the first imperial dynasty of China(not to be confused with the Qing Dynasty, the last dynasty of China). So Ying Zheng named himself "Qin Shi Huang" because "Shi Huang" means "the first emperor" in Chinese.

Qin Shi Huang undertook gigantic projects, including the first version of the Great Wall of China, the now famous city-sized mausoleum guarded by a life-sized Terracotta Army, and a massive national road system. There is a story about the road system:
There were n cities in China and Qin Shi Huang wanted them all be connected by n-1 roads, in order that he could go to every city from the capital city Xianyang.
Although Qin Shi Huang was a tyrant, he wanted the total length of all roads to be minimum,so that the road system may not cost too many people's life. A daoshi (some kind of monk) named Xu Fu told Qin Shi Huang that he could build a road by magic and that magic road would cost no money and no labor. But Xu Fu could only build ONE magic road for Qin Shi Huang. So Qin Shi Huang had to decide where to build the magic road. Qin Shi Huang wanted the total length of all none magic roads to be as small as possible, but Xu Fu wanted the magic road to benefit as many people as possible ---- So Qin Shi Huang decided that the value of A/B (the ratio of A to B) must be the maximum, which A is the total population of the two cites connected by the magic road, and B is the total length of none magic roads.
Would you help Qin Shi Huang?
A city can be considered as a point, and a road can be considered as a line segment connecting two points.
 

Input
The first line contains an integer t meaning that there are t test cases(t <= 10).
For each test case:
The first line is an integer n meaning that there are n cities(2 < n <= 1000).
Then n lines follow. Each line contains three integers X, Y and P ( 0 <= X, Y <= 1000, 0 < P < 100000). (X, Y) is the coordinate of a city and P is the population of that city.
It is guaranteed that each city has a distinct location.
 

Output
For each test case, print a line indicating the above mentioned maximum ratio A/B. The result should be rounded to 2 digits after decimal point.
 

Sample Input
2 4 1 1 20 1 2 30 200 2 80 200 1 100 3 1 1 20 1 2 30 2 2 40
 

Sample Output
65.00 70.00
 
#include <cstdio>
#include <cstring>
#include <vector>
#include <cmath>
#include <queue>
using namespace std;
const int maxn=1<<30;
int n;
int coor[1010][2];
double pri[1010];
double G[1010][1010];
bool used[1010];
double lowc[1010];
double Max[1010][1010];
double get_dis(int x1,int y1,int x2,int y2){
    return sqrt((x1-x2)*(x1-x2)*1.0+(y1-y2)*(y1-y2)*1.0);
}
double cnt;
struct node{
    int v;
    double w;
    node(int vv,double ww):v(vv),w(ww){}
    bool operator<(const node &other)const{
        return w>other.w;
    }
};
void prim(){
    memset(Max,0,sizeof(Max));
    memset(used,0,sizeof(used));
    int pre[1010];
    cnt=0;
    for(int i=0;i<=n;i++){
        lowc[i]=G[1][i];
    }
    priority_queue<node> q;
    q.push(node(1,0));
    pre[1]=0;
    while(!q.empty()){
        node p=q.top();
        q.pop();
        int u=p.v;
        if(used[u])continue;
        used[u]=true;
        cnt+=p.w;
        for(int i=1;i<=n;i++){
            if(used[i]&&i!=u)
                Max[u][i]=Max[i][u]=max(Max[i][pre[u]],lowc[u]);
            if(used[i]||lowc[i]<G[u][i])continue;
            lowc[i]=G[u][i];
            q.push(node(i,lowc[i]));
            pre[i]=u;
        }
    }
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        memset(G,0,sizeof(G));
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d%d%lf",&coor[i][0],&coor[i][1],&pri[i]);
        for(int i=0;i<=n;i++)
            G[i][i]=0;
        for(int i=1;i<=n;i++)
            for(int j=i+1;j<=n;j++)
                G[i][j]=G[j][i]=get_dis(coor[i][0],coor[i][1],coor[j][0],coor[j][1]);
        prim();
        double ans=-1;
        for(int i=1;i<=n;i++)
        for(int j=i+1;j<=n;j++){
            if((pri[i]+pri[j])/(cnt-Max[i][j])>ans)
                    ans=(pri[i]+pri[j])/(cnt-Max[i][j]);
        }
        printf("%.2lf\n",ans);
    }
}
求出了次小生成树中需要的Max数组,修改下即可求得次小生成树

<think>我们正在处理用户的问题:如何将时间转换为 qint64 类型。 根据引用[1]:QDateTime本质上是一个qint64。并且引用[1]中提到了一个静态函数:static QDateTime fromMSecsSinceEpoch(qint64 msecs); 反过来,我们可以使用QDateTime的方法来将时间转换为qint64(即自1970-01-01T00:00:00 UTC以来的毫秒数)。 在Qt中,将时间(QDateTime)转换为qint64(毫秒数)可以使用toMSecsSinceEpoch()函数。 步骤: 1. 获取当前时间(或任意QDateTime对象) 2. 调用toMSecsSinceEpoch()函数,返回qint64类型的毫秒数。 示例代码: ```cpp #include <QDateTime> #include <QDebug> int main() { // 获取当前时间 QDateTime currentDateTime = QDateTime::currentDateTime(); // 转换为自1970-01-01 00:00:00 UTC以来的毫秒数 qint64 msecs = currentDateTime.toMSecsSinceEpoch(); qDebug() << "Current datetime in milliseconds since epoch:" << msecs; return 0; } ``` 另外,如果用户有一个特定的时间字符串,需要先将其转换为QDateTime,然后再转换为qint64。例如: ```cpp QString timeString = "2023-01-01 12:00:00"; QDateTime dateTime = QDateTime::fromString(timeString, "yyyy-MM-dd HH:mm:ss"); if (dateTime.isValid()) { qint64 msecs = dateTime.toMSecsSinceEpoch(); qDebug() << "The datetime in milliseconds since epoch:" << msecs; } else { qDebug() << "Invalid datetime string"; } ``` 注意:QDateTime的时区处理。默认情况下,QDateTime::currentDateTime()返回本地时间,而toMSecsSinceEpoch()会自动将其转换为UTC时间。如果用户的时间字符串是本地时间,则不需要额外处理,因为fromString默认会解释为本地时间。如果时间字符串是UTC时间,则需要设置时区为UTC。 例如,设置时区为UTC: ```cpp dateTime.setTimeSpec(Qt::UTC); // 在调用toMSecsSinceEpoch()之前设置 ``` 或者,在解析字符串时指定时区: ```cpp QDateTime dateTime = QDateTime::fromString(timeString, "yyyy-MM-dd HH:mm:ss", Qt::UTC); ``` 总结:使用QDateTime的toMSecsSinceEpoch()函数将时间转换为qint64(毫秒数)。 如果用户需要秒数(而不是毫秒数),则可以使用toSecsSinceEpoch()(返回自纪元以来的秒数,qint64类型),但注意这是Qt 5.8引入的。 根据用户需求,我们提供毫秒数的转换。 如果用户使用的是较旧的Qt版本(低于5.2),则可以使用toMSecsSinceEpoch()的替代方法:先转换为QDateTime(表示UTC时间),然后使用toTime_t()乘以1000再加上毫秒部分,但这样比较复杂。推荐使用toMSecsSinceEpoch(),因为它简洁且准确。 注意:toMSecsSinceEpoch()在Qt 4.7中引入。 因此,在Qt 4.7及以上版本中,我们可以安全地使用toMSecsSinceEpoch()。 如果用户需要将时间转换为qint64类型的秒数(而不是毫秒数),则可以使用toSecsSinceEpoch()(Qt 5.8引入)。 根据用户问题,我们提供毫秒数的转换(qint64类型)。 如果用户有关于时间格式的特定问题,请参考QDateTime的文档。 最后,我们生成相关问题。</think>### Qt 中将时间转换为 qint64 类型的方法 在 Qt 中,将时间转换为 `qint64` 类型(64 位整数)主要通过 `QDateTime` 类实现,核心是使用 `toMSecsSinceEpoch()` 函数。以下是详细步骤: #### 1. **获取当前时间并转换** ```cpp #include <QDateTime> // 获取当前时间 QDateTime currentTime = QDateTime::currentDateTime(); // 转换为 qint64(毫秒数,自 1970-01-01 UTC 起) qint64 timestamp = currentTime.toMSecsSinceEpoch(); ``` #### 2. **转换特定时间字符串** ```cpp QString timeString = "2023-01-01 12:00:00"; QDateTime specificTime = QDateTime::fromString(timeString, "yyyy-MM-dd HH:mm:ss"); if (specificTime.isValid()) { qint64 timestamp = specificTime.toMSecsSinceEpoch(); } ``` #### 3. **时区处理** ```cpp // 指定 UTC 时区 QDateTime utcTime = QDateTime::currentDateTimeUtc(); qint64 utcTimestamp = utcTime.toMSecsSinceEpoch(); // 本地时区转换 QDateTime localTime = QDateTime::fromMSecsSinceEpoch(utcTimestamp, Qt::LocalTime); ``` #### 关键点说明: - **`toMSecsSinceEpoch()`**:返回自 **1970-01-01 00:00:00 UTC** 起的毫秒数(`qint64` 类型) - **时区处理**:`QDateTime` 默认使用本地时区,可通过 `setTimeSpec()` 或 `toUTC()` 调整 - **有效性检查**:转换前需用 `isValid()` 验证时间数据 - **精度**:`qint64` 可表示约 **2.92 亿年** 的时间范围,无溢出风险 > 引用说明:`QDateTime` 本质是 `qint64` 类型存储的时间戳,通过 `toMSecsSinceEpoch()` 直接获取数值表示[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值