多校1.Abandoned country1001

本文介绍了一种算法,通过构建最小生成树解决村庄间的道路重建问题,并计算任意两个村庄间路径的平均长度。使用Kruskal算法寻找最小生成树,并通过DFS计算每条边的贡献度。

点击打开链接

Problem Description
An abandoned country has  n(n100000)  villages which are numbered from 1 to  n . Since abandoned for a long time, the roads need to be re-built. There are  m(m1000000)  roads to be re-built, the length of each road is  wi(wi1000000) . Guaranteed that any two  wi  are different. The roads made all the villages connected directly or indirectly before destroyed. Every road will cost the same value of its length to rebuild. The king wants to use the minimum cost to make all the villages connected with each other directly or indirectly. After the roads are re-built, the king asks a men as messenger. The king will select any two different points as starting point or the destination with the same probability. Now the king asks you to tell him the minimum cost and the minimum expectations length the messenger will walk.
 

Input
The first line contains an integer  T(T10)  which indicates the number of test cases. 

For each test case, the first line contains two integers  n,m  indicate the number of villages and the number of roads to be re-built. Next  m  lines, each line have three number  i,j,wi , the length of a road connecting the village  i  and the village  j  is  wi .
 

Output
output the minimum cost and minimum Expectations with two decimal places. They separated by a space.
 

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

Sample Output
  
6 3.33
 



题意: 给边求最小生成树,并且求任意两点的距离平均值(和/Cn2)

思路:因为点数太多只能用kruskal先求出最小生成树(同时保存好每个点与之相连的点和边权)。再用dfs递归出每条边左边右边的结点数(边的贡献度nl*nr=该边被经过的次数)还用到了dp,dp公式是以点为中心,dp[i] = 与该点相连的边(除了与父亲节点相连的边)的贡献度和*边权。


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>

using namespace std;

const int MAX_V = 100000 + 10;

int n,m;
int father[MAX_V],sum[MAX_V];
double dp[MAX_V];

struct node
{
    int fr, to, va;
    node(){};
    node(int x, int y, int z):fr(x),to(y),va(z){}
};

struct Node
{
    int to, va;
    Node(){};
    Node(int x, int y) : to(x),va(y){}
};

vector<Node> vet[MAX_V];
vector<node> edge;

bool comp(node a, node b)
{
    return a.va < b.va;
}

int ffather(int x)
{
    if(father[x] == x)
        return x;
    else
        return father[x] = ffather(father[x]);
}

long long Kruskal()
{
    sort(edge.begin(), edge.end(), comp);
    for(int i = 1; i <= n; i++)
        father[i] = i;
    long long add = 0;
    for(int i = 0; i < m; i++)
    {
        int fr = edge[i].fr, to = edge[i].to, va = edge[i].va;
        fr = ffather(fr);
        to = ffather(to);
        if(fr == to)
            continue;
        father[fr] = to;
        add += (long long)va;
        vet[edge[i].fr].push_back(Node(edge[i].to, va));
        vet[edge[i].to].push_back(Node(edge[i].fr, va));
    }
    return add;
}

void dfs(int root, int father)
{
    sum[root] = 1;                                                   //一边的点
    for(int i = 0; i < (int)vet[root].size(); i++)
    {
        
        Node temp = vet[root][i];
        if(temp.to == father)                                        //排除掉父亲结点
            continue;
        dfs(temp.to,root);
        sum[root] += sum[temp.to];
        dp[root] += dp[temp.to] + ((double)sum[temp.to]*(n-sum[temp.to])) * temp.va;
    }

}





int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d", &n, &m);
        edge.clear();                                            //初始化..
        for (int i = 0, a, b, c; i < m; i++)
        {
            scanf("%d%d%d", &a, &b, &c);
            edge.push_back(node(a, b, c));
        }
        memset(dp,0,sizeof(dp));
        for(int i = 1;i <= n;i++)vet[i].clear();                   // 不能忘阿...
        printf("%I64d ",Kruskal());
        dfs(1,0);
        long long s = (long long)n * (n - 1) / 2;
        printf("%.2f\n",dp[1]/(double)s);
    }
    return 0;
}







### Java中 `IllegalArgumentException: Surface was abandoned` 的原因及解决方案 在使用 Android 的 Camera2 API 时,如果出现 `java.lang.IllegalArgumentException: Surface was abandoned` 异常,通常是由于目标 Surface 已经被释放或无效,而此时仍尝试将其用于创建捕获会话或其他操作。以下是详细的原因分析和解决方法: #### 原因分析 1. **Surface 被提前释放** 当 Surface 对象(例如来自 `SurfaceTexture` 或 `SurfaceView`)被释放后,再尝试使用它会导致此异常。例如,在调用 `setRepeatingRequest` 或 `createCaptureSession` 方法之前,Surface 已经被销毁[^1]。 2. **生命周期管理不当** 在 Android 应用中,组件的生命周期(如 Activity 或 Fragment)需要与 Camera2 API 的资源生命周期同步。如果 Surface 在 Activity 或 Fragment 销毁时未正确关闭,或者在重新初始化时未正确重建,可能导致 Surface 失效[^3]。 3. **参数传递错误** 如果在某些方法调用中传递了非法或不匹配的参数,也可能引发 `IllegalArgumentException` 异常。例如,传递给 `OutputConfiguration` 构造函数的 Surface 参数可能为空或已释放[^2]。 #### 解决方案 1. **确保 Surface 生命周期一致性** 在使用 Camera2 API 时,必须确保 Surface 的生命周期与相机资源的生命周期一致。可以在以下关键点进行检查: - 在 `onSurfaceTextureAvailable` 回调中创建 Surface。 - 在 `onSurfaceTextureDestroyed` 回调中释放 Surface 并关闭相机会话。 ```java @Override public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) { Surface surface = new Surface(surfaceTexture); // 使用 surface 创建捕获会话 } @Override public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) { if (cameraCaptureSession != null) { cameraCaptureSession.close(); } return true; } ``` 2. **检查 Surface 状态** 在将 Surface 传递给 Camera2 API 之前,确保其状态有效。可以通过以下方式验证: ```java if (surface == null || !surface.isValid()) { throw new IllegalStateException("Surface is invalid or null"); } ``` 3. **避免异步问题** 如果在线程环境中操作 Surface 或相机资源,可能会导致竞态条件。建议在主线程中处理所有与 Surface 和相机相关的操作,或通过同步机制确保线程安全。 4. **清理资源** 在 Activity 或 Fragment 销毁时,确保所有相关资源(如 Surface、CameraDevice、CameraCaptureSession)都被正确释放。例如: ```java @Override protected void onDestroy() { super.onDestroy(); if (cameraDevice != null) { cameraDevice.close(); } if (surface != null) { surface.release(); } } ``` 5. **调试与日志** 在开发过程中,可以添加日志以跟踪 Surface 和相机资源的状态变化。例如: ```java Log.d("Camera2Debug", "Surface created: " + surface.isValid()); ``` --- ### 示例代码 以下是一个完整的示例,展示如何正确管理 Surface 和 Camera2 API 的生命周期: ```java private SurfaceTexture surfaceTexture; private Surface surface; @Override public void onSurfaceTextureAvailable(SurfaceTexture texture, int width, int height) { surfaceTexture = texture; surface = new Surface(surfaceTexture); try { cameraDevice.createCaptureSession(Arrays.asList(surface), new CameraCaptureSession.StateCallback() { @Override public void onConfigured(CameraCaptureSession session) { cameraCaptureSession = session; // 配置捕获请求 } @Override public void onConfigureFailed(CameraCaptureSession session) { Log.e("Camera2Error", "Capture session configuration failed"); } }, null); } catch (CameraAccessException e) { e.printStackTrace(); } } @Override public boolean onSurfaceTextureDestroyed(SurfaceTexture texture) { if (cameraCaptureSession != null) { cameraCaptureSession.close(); } if (surface != null) { surface.release(); } return true; } ``` --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值