http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=45523

有一个国王想在首都与各个城市之间修建公路,但是他的预算太高,所以必须要降低预算。

为了降低预算,必须要有新计划,新计划必须满足每两个城市都连通,首都和城市的最短距离不会改变两个条件。

输入N各城市,首都标号为1,m条路,m行每行四个数,u,v,d,c;表示u跟v联通,并且距离为d,修路花费为c。

输出最小花费。

首先从首都开始求出到每个城市的最短路,然后再满足最短距离的情况下,更新最小花费。

 #include <iostream>
#include <vector>
#include <queue>
#include <cstdio>
using namespace std; const int maxn = ;
const int INF = <<;
struct edge {
int to,cost,distance;
edge(){}
edge( int x,int y,int z ) {
to=x;
distance=y;
cost=z;
}
}; typedef pair<int,int>P;
vector<edge>G[maxn];
int d[maxn];
int N; void dijkstra(int s) {
priority_queue<P,vector<P>,greater<P> >que;
for(int i=;i<=N;i++) d[i]=INF;
d[s]=;
que.push(P(,s)); while(!que.empty()) {
P p=que.top(); que.pop();
int v=p.second;
if(d[v]<p.first) continue;
for(int i=;i<G[v].size();i++) {
edge e=G[v][i];
if(d[e.to]>d[v]+e.distance) {
d[e.to]=d[v]+e.distance;
que.push(P(d[e.to],e.to));
}
}
}
} int main()
{
//freopen("a.txt","r",stdin);
int M;
while(~scanf("%d%d",&N,&M)&&(N+M)) {
for(int i=;i<=N;i++) G[i].clear();
int a,b,c,v;
for(int i=;i<M;i++) {
scanf("%d%d%d%d",&a,&b,&c,&v);
// printf("%d %d %d %d\n",a,b,c,v);
G[a].push_back(edge(b,c,v));
G[b].push_back(edge(a,c,v));
}
dijkstra();
// for(int i=1;i<=N;i++) printf("%d\n",d[i]);
int sum=;
for(int i=;i<=N;++i) {
int min_cost=INF;
for(int j=;j<G[i].size();++j) {
edge &e=G[i][j];
// printf("%d %d %d\n",e.to,d[e.to],e.distance);
if(d[e.to]+e.distance==d[i]&&e.cost<min_cost)
{
min_cost=e.cost;
}
}
sum+=min_cost;
}
printf("%d\n",sum);
}
return ;
}

AOJ-2249 Road Construction(最短路)的更多相关文章

  1. AOJ 2249 Road Construction(Dijkstra+优先队列)

    [题目大意] http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2249 [题目大意] 一张无向图,建造每条道路需要的费用已经给出, 现 ...

  2. AOJ 2249 Road Construction (dijkstra)

    某国王需要修路,王国有一个首都和多个城市,需要修路.已经有修路计划了,但是修路费用太高. 为了减少修路费用,国王决定从计划中去掉一些路,但是需要满足一下两点: 保证所有城市都能连通 所有城市到首都的最 ...

  3. Aizu - 2249 Road Construction

    题目:给出若干个建筑之间的一些路,每条路都有对应的长度和需要的花费,问在保证源点1到其他个点的距离最短的情况下,最少的花费是多少/ 思路:和一般的最短路问题相比,多了一个 数组id[i],用来记录到达 ...

  4. 【Aizu - 2249】Road Construction(最短路 Dijkstra算法)

    Road Construction Descriptions Mercer国王是ACM王国的王者.他的王国里有一个首都和一些城市.令人惊讶的是,现在王国没有道路.最近,他计划在首都和城市之间修建道路, ...

  5. POJ3352 Road Construction(边双连通分量)

                                                                                                         ...

  6. POJ3352 Road Construction (双连通分量)

    Road Construction Time Limit:2000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u Sub ...

  7. poj 3352 Road Construction【边双连通求最少加多少条边使图双连通&&缩点】

    Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10141   Accepted: 503 ...

  8. POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)

    POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的.一份代码能交.给定一个连通无向图,问加几条边能使得图变成一个双连通图 ...

  9. POJ3352 Road Construction 双连通分量+缩点

    Road Construction Description It's almost summer time, and that means that it's almost summer constr ...

随机推荐

  1. JS 学习笔记--10---基本包装类型

    练习中使用的浏览器是IE10,如果有什么错误或者不同意见,希望各位朋友能够指正,练习代码附在后面 1.基本包装类型:    首先是基本类型,但又是特殊的引用类型,因为他们可以调用系统的方法,这种类型就 ...

  2. bzoj 2761 平衡树

    裸地平衡树,只需要用到find操作 /**************************************************************     Problem:     U ...

  3. 【POJ】【1635】Subway Tree Systems

    树的最小表示法 给定两个有根树的dfs序,问这两棵树是否同构 题解:http://blog.sina.com.cn/s/blog_a4c6b95201017tlz.html 题目要求判断两棵树是否是同 ...

  4. 【BZOJ】【3522】【POI2014】Hotel

    暴力/树形DP 要求在树上找出等距三点,求方案数,那么用类似Free Tour2那样的合并方法,可以写出: f[i][j]表示以 i 为根的子树中,距离 i 为 j 的点有多少个: g[i][j]表示 ...

  5. Leetcode#106 Construct Binary Tree from Inorder and Postorder Traversal

    原题地址 二叉树基本操作 [       ]O[              ] [       ][              ]O 代码: TreeNode *restore(vector<i ...

  6. NENU_CS_segment_tree

    单点更新 http://acm.hdu.edu.cn/showproblem.php?pid=1166 题意:单点更新加减,区间查询求和. #include<cstdio> #define ...

  7. [百度空间] [转]程序员趣味读物:谈谈Unicode编码

    出处:CSDN [ 2005-05-13 10:05:53 ] 作者:fmddlmyy 这是一篇程序员写给程序员的趣味读物.所谓趣味是指可以比较轻松地了解一些原来不清楚的概念,增进知识,类似于打RPG ...

  8. windows phone中ListBox的简单使用

    学习windows phone数据绑定的一点点心得,在wp系统的APP中经常遇到这样风格的软件,那它们到底怎样实现的呢?我就大致去做了一下,比较粗虐,但基本的都已经有了,实现后的结果为: 哇,这个图截 ...

  9. BZOJ 1088

    真是智商不够, 智商题:.... 假如:第1,2个格子已知,然后根据第二列的情况,就可以把所有满足的情况推出来,又萌萌哒.. 无耻攒字数: #include<stdio.h> using ...

  10. Http Url Get请求方式需要对中文参数进行编码

    public static void main(String[] args) { try { String mytext = java.net.URLEncoder.encode("上海南站 ...