ccf 交通规划

2023-05-16

201609-4
试题名称:交通规划
时间限制:1.0s
内存限制:256.0MB
问题描述:
问题描述
  G国国王来中国参观后,被中国的高速铁路深深的震撼,决定为自己的国家也建设一个高速铁路系统。
  建设高速铁路投入非常大,为了节约建设成本,G国国王决定不新建铁路,而是将已有的铁路改造成高速铁路。现在,请你为G国国王提供一个方案,将现有的一部分铁路改造成高速铁路,使得任何两个城市间都可以通过高速铁路到达,而且从所有城市乘坐高速铁路到首都的最短路程和原来一样长。请你告诉G国国王在这些条件下最少要改造多长的铁路。
输入格式
  输入的第一行包含两个整数 n, m,分别表示G国城市的数量和城市间铁路的数量。所有的城市由1到 n编号,首都为1号。
  接下来 m行,每行三个整数 a, b, c,表示城市 a和城市 b之间有一条长度为 c的双向铁路。这条铁路不会经过 ab以外的城市。
输出格式
  输出一行,表示在满足条件的情况下最少要改造的铁路长度。
样例输入
4 5
1 2 4
1 3 5
2 3 2
2 4 3
3 4 2
样例输出
11
评测用例规模与约定
  对于20%的评测用例,1 ≤ n ≤ 10,1 ≤ m ≤ 50;
  对于50%的评测用例,1 ≤ n ≤ 100,1 ≤ m ≤ 5000;
  对于80%的评测用例,1 ≤ n ≤ 1000,1 ≤ m ≤ 50000;
  对于100%的评测用例,1 ≤ n ≤ 10000,1 ≤ m ≤ 100000,1 ≤ a, b ≤ n,1 ≤ c ≤ 1000。输入保证每个城市都可以通过铁路达到首都。
答题栏
试题编号:201609-4
试题名称:交通规划
编译环境:
答案程序:
提交确认:以下必须全部满足才能提交:


迪杰特斯拉求单源最短路,并且路径权值最小。

这里本来用的是裸的迪杰特斯拉无任何优化的,结果莫名wa,很难受,不过也学习到了一种完美的迪杰特斯拉模板,用的是优先队列,这里纪念一下。

自己的比较乱的代码:

#include <stdio.h>  
#include <string.h>  
#include <string>  
#include <iostream>  
#include <stack>  
#include <queue>  
#include <vector>  
#include <algorithm>  
using namespace std;  
const int inf = 999999999;
struct node{
	int v;
	int d;
	node(int vv=0, int dd=0)
	{
		v=vv; d=dd;
	}
	friend bool operator < (const node a,const node b)
	{
		return a.d > b.d;
	}
}; // 点与边 
int n,m;
int dis[10010];
int cost[10010];
bool book[10010];
vector<node> mmp[10010];
void dij()
{
	priority_queue<node> op;
	memset(book, false, sizeof(book));
	
	for(int i=2; i<=n; i++)
    dis[i] = cost[i] = inf;	
	op.push(node(1,0));
	book[1] = 1;
	while(!op.empty())
	{
       node tmp = op.top(); op.pop();
	   if(!book[tmp.v]) book[tmp.v] = 1;		
	   int len = mmp[tmp.v].size();
	   for(int i=0; i<len; i++)
	   {
	   	  int vv = mmp[tmp.v][i].v;
	   	  if(book[vv])continue;
	   	  if(dis[vv] > dis[tmp.v] + mmp[tmp.v][i].d){dis[vv] = dis[tmp.v] + mmp[tmp.v][i].d;cost[vv]=mmp[tmp.v][i].d;op.push(node(vv,dis[vv]));}
	   	  if(dis[vv] == dis[tmp.v] + mmp[tmp.v][i].d)
	   	  {
	   	     cost[vv] = min(cost[vv],mmp[tmp.v][i].d);	
		  }
	   }		
	}	
	return ;
}
int main()  
{
     cin >> n >> m;
     int a,b,c;
     for(int i=1; i<=m; i++)
     {
     	scanf("%d %d %d",&a, &b, &c);
     	mmp[a].push_back(node(b,c));
     	mmp[b].push_back(node(a,c));
	 }
	 dij();
	 int sum = 0;
	 for(int i=1; i<=n; i++) sum += cost[i];
	 cout << sum << endl;
    return 0;  
}  


这里是大神完美的模板:

#include <iostream>
#include <queue>
#include <vector>

#define NMAX 10005
#define INTMAX 0x7fffffff

using namespace std;

// v表示节点,cost表示出发点到v点的距离
struct Node {
    int v; 
    int cost;
    Node(int vv = 0, int c = 0) {
        v = vv, cost = c;
    }
    // 优先队列将按距离从小到大排列
    friend bool operator<(Node n1, Node n2) {
        return n1.cost > n2.cost;
    }
};

// v表示边的另一端节点,cost表示该边的权重
struct Edge {
    int v;
    int cost;
    Edge(int vv = 0, int c = 0) {
        v = vv, cost = c;
    }
};

vector<Edge>G[NMAX];    // 无向图
bool marked[NMAX];      // D算法中每个顶点仅处理一遍
int disto[NMAX];        // 出发点到某点距离
int costo[NMAX];        // 接通该点需要增加的边的权重
int N, M;

void dijkstra(int s) {
    for (int i = 0; i <= N; i++) {
        costo[i] = disto[i] = INTMAX;//初始化 
        marked[i] = false;
    }
    disto[s] = 0;
    costo[s] = 0;
    priority_queue<Node>pq;     // 保存<v,disto[v]>且按disto[v]升序排列
    pq.push(Node(s, 0));
    marked[0]=true;

    Node tmp;
    while (!pq.empty()) {
        tmp = pq.top();
        pq.pop();
        int v = tmp.v;
        if (!marked[v]) {
            marked[v]=true;
            int len = G[v].size();
            for (int i = 0; i < len; i++) {
                int vv = G[v][i].v;
                if(marked[vv])
                    continue;
                int cost = G[v][i].cost;
                int newdist = disto[v] + cost;
                if (disto[vv] > newdist) {
                    disto[vv] = newdist;
                    costo[vv] = cost;   // 增加的内容
                    pq.push(Node(vv, disto[vv]));
                }
                // 加入点vv时若出现多种距离相同的方案,选取新边最小那个
                if (disto[vv] == newdist) {
                    costo[vv] = min(costo[vv], cost);
                }
            }
        }
    }
}

int main(void) {
    cin >> N >> M;

    int s, e, c;
    for (int i = 0; i < M; i++) {
        cin >> s >> e >> c;
        G[s].push_back(Edge(e, c));//无线图中添加边 
        G[e].push_back(Edge(s, c));
    }
    dijkstra(1);

    // 统计边权重
    int res = 0;
    for (int i = 2; i <= N; i++) {
        res += costo[i];
    }
    cout << res << endl;

    return 0;
}
以后还是习惯用这种完美的方法吧。


本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ccf 交通规划 的相关文章

随机推荐

  • Android 通知使用权

    1 创建service extends NotificationListenerService xff0c 并实现onNotificationRemoved onNotificationPosted public class Notific
  • Android 系统文件浏览器

    1 调用系统文件浏览器 Intent intent 61 new Intent Intent ACTION GET CONTENT intent setType 34 34 设置类型 xff0c 我这里是任意类型 xff0c 任意后缀的可以
  • Android 导入导出excel xls、xlsx

    1 导入依赖 implementation group 39 org apache poi 39 name 39 poi ooxml 39 version 39 3 17 39 implementation group 39 org apa
  • Android 导出PDF PdfDocument

    导出PDF 64 param view 要导出的view xff0c 如果view高度过高 xff08 超过一屏的高度 xff09 xff0c 在改view外部套一层Scrollview即可 如果要导出列表类型View 比如Listview
  • Android 获取所有短信-彩信

    1 权限 lt 读取短信 gt lt uses permission android name 61 34 android permission READ SMS 34 gt lt uses permission android name
  • Photoshop CC 2018 安装包安装教程

    Photoshop CC 2018功能特点 1 更紧密连接的 Photoshop 全新的智慧型锐利化 2 智慧型增加取样 内含 Extended 功能 Camera RAW 8 和图层支援 3 可编辑的圆角矩形 多重形状和路径选择 相机防手
  • 【原创】什么是原码、反码、补码?

    原码 反码 补码是计算机中对数字的二进制表示方法 原码 xff1a 将最高位作为符号位 xff08 0表示正 xff0c 1表示负 xff09 xff0c 其它数字位代表数值本身的绝对值的数字表示方式 反码 xff1a 如果是正数 xff0
  • snprintf中的错误,不要出现目标地址也是源地址的情况

    在使用snprintf时 xff0c 千万要注意一点 xff0c 不要出现目标地址也是源地址的情况 看如下例子 xff1a span class token macro property span class token directive
  • Linux下的shell进度条

    一 关于Shell Shell的作用是解释执行用户的命令 xff0c 它有两种执行命令的方式 xff1a 交互式和批处理 Shell脚本和编程语言很相似 xff0c 也有变量和流控制语句 xff0c 但Shell脚本是解释执行 xff0c
  • you-get相关使用命令

    you get i url 获取视频格式 清晰度等信息 you get o E folder url 保存路径 在当前目录的路径栏 输入cmd即可打开命令行 或者 shift 右键 用powershell打开 you get p PotPl
  • Spring Cloud从入门到放弃(四):Eureka的常用配置

    前言 Spring Cloud系列 点击查看Spring Cloud系列文章 eurka的常用配置 eureka server前缀的配置项 是否允许开启自我保护模式 xff0c 缺省 xff1a span class token boole
  • STM32之点亮LED

    学习一个新的处理器 xff0c 第一个程序肯定就是点亮LED xff0c 它可以让我们较快的 较清晰的了解到一个处理器的程序结构 xff0c 学习32也不例外 xff0c 首先第一个程序我们就来点亮LED xff0c 点亮LED程序有很多种
  • 判断两条线段是否相交

    判断两条直线是否相交有两步 xff1a 1 xff1a 快速排斥 xff08 可以筛除大部分 xff09 2 xff1a 跨立试验 xff08 下面会有所说明 xff09 现在开始解释 xff1a 第一步快速排斥 xff0c 实际上就是先判
  • 2015-2016 ACM-ICPC Pacific Northwest Regional Contest Div.2( Problem V Gears)

    题目地址 xff1a 点击打开链接 题意 xff1a 给你很多齿轮 xff0c 让你判断第一个齿轮和第n个齿轮的关系 有三种关系题目中已经给出 解题思路 xff1a 算是比较直观的一个dfs题目了 xff0c 重点是怎么样处理这个dfs 结
  • 免费馅饼(简单动态规划)

    都说天上不会掉馅饼 xff0c 但有一天gameboy正走在回家的小径上 xff0c 忽然天上掉下大把大把的馅饼 说来gameboy的人品实在是太好了 xff0c 这馅饼别处都不掉 xff0c 就掉落在他身旁的10米范围内 馅饼如果掉在了地
  • CF816B-Karen and Coffee

    B Karen and Coffee time limit per test2 5 seconds memory limit per test512 megabytes inputstandard input outputstandard
  • B. Mister B and Angle in Polygon 421.div2

    B Mister B and Angle in Polygon time limit per test 2 seconds memory limit per test 256 megabytes input standard input o
  • openwrt下安装lighttpd/webdav模块及改变安装目录

    Openwrt下安装lighttpd及Webdav模块 安装lightttpd 1 opkg update 2 opkg install lighttpd 依赖libxml库 3 修改 etc lighttpd lighttpd conf
  • Game of the Rows CodeForces - 839B

    Daenerys Targaryen has an army consisting of k groups of soldiers the i th group contains ai soldiers She wants to bring
  • ccf 交通规划

    201609 4试题名称 xff1a 交通规划时间限制 xff1a 1 0s内存限制 xff1a 256 0MB问题描述 xff1a 问题描述 G国国王来中国参观后 xff0c 被中国的高速铁路深深的震撼 xff0c 决定为自己的国家也建设