201609-4 |
试题名称: | 交通规划 |
时间限制: | 1.0s |
内存限制: | 256.0MB |
问题描述: |
问题描述
G国国王来中国参观后,被中国的高速铁路深深的震撼,决定为自己的国家也建设一个高速铁路系统。
建设高速铁路投入非常大,为了节约建设成本,G国国王决定不新建铁路,而是将已有的铁路改造成高速铁路。现在,请你为G国国王提供一个方案,将现有的一部分铁路改造成高速铁路,使得任何两个城市间都可以通过高速铁路到达,而且从所有城市乘坐高速铁路到首都的最短路程和原来一样长。请你告诉G国国王在这些条件下最少要改造多长的铁路。
输入格式
输入的第一行包含两个整数
n,
m,分别表示G国城市的数量和城市间铁路的数量。所有的城市由1到
n编号,首都为1号。
接下来
m行,每行三个整数
a,
b,
c,表示城市
a和城市
b之间有一条长度为
c的双向铁路。这条铁路不会经过
a和
b以外的城市。
输出格式
输出一行,表示在满足条件的情况下最少要改造的铁路长度。
样例输入
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。输入保证每个城市都可以通过铁路达到首都。
|
迪杰特斯拉求单源最短路,并且路径权值最小。
这里本来用的是裸的迪杰特斯拉无任何优化的,结果莫名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(使用前将#替换为@)