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. 06.Hibernate实体类生命周期

        前言:Session接口是Hibernate向应用程序提供的操作数据库的主要接口,它提供了基本的增删查改方法,而且Session具有一个缓存它是Hibernate的一级缓存.站在持久化层的角度 ...

  2. 强力重置ASP.NET membership加密后的密码![转]

    公司网站的用户管理采用的是ASP.NET内置的membership管理,在web.config文件中的密码格式配置是加密了的,passwordFormat="Hashed",这样在 ...

  3. Kafka的coordinator

    (基于0.10版本) Group Management Protocol Kafka的coordiantor要做的事情就是group management,就是要对一个团队(或者叫组)的成员进行管理. ...

  4. no module named firefly.master.master

    因为没有安装firefly python setup.py install

  5. JavaScript文件应该放在网页的什么位置

    JavaScript文件应该放在网页的什么位置 http://www.lihuai.net/qianduan/js/352.html 在<良好的JavaScript编程习惯>系列教程的第三 ...

  6. iOS第三方支付-银联支付

    #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> #import "UPPayPluginDeleg ...

  7. POJ 1940

    #include <iostream> #include <iomanip> #define MAXN 1000 using namespace std; struct nod ...

  8. POJ 1419

    #include <iostream> #define MAXN 105 #define max _max using namespace std; int j; bool _m[MAXN ...

  9. Windows 7 常用快捷键 命令

    Win+E:  打开新的windows资源浏览器 Win+F:搜索文件或文件夹 Win+R:打开运行窗口 Win + D:显示桌面 Win + M:最小化所有窗口 Ctrl+Shift+N: 新建文件 ...

  10. redis rdb

    http://blog.chinaunix.net/uid-1757778-id-3977331.html