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. [转载]Extjs中的dom,Ext.Element和Ext.Component对象的关系

    原文地址:http://www.cnblogs.com/lwzz/archive/2011/01/30/1948106.html   Ext.Element对象是对dom对象的封装,目的是为了跨平台以 ...

  2. 【POJ】【1637】Sightseeing tour

    网络流/最大流 愚人节快乐XD 这题是给一个混合图(既有有向边又有无向边),让你判断是否有欧拉回路…… 我们知道如果一个[连通]图中每个节点都满足[入度=出度]那么就一定有欧拉回路…… 那么每条边都可 ...

  3. android控件---自定义带文本的ImageButton

    由于SDK提供的ImageButton只能添加图片,不能添加文字:而Button控件添加的文字只能显示在图片内部:当我们需要添加文字在图片外部时就不能满足我们的需求了,顾只能自己写个自定义ImageB ...

  4. Effective Java总结

    规则1. 用静态工厂方法代替构造器 例子: public class Example { } public class StaticFactory { //valueOf/Of/getInstance ...

  5. 关于High-Resolution Timer(了解)

    如果一个系统包含高精度性能计数器(HRPC,high-resolution performance counter)则此系统提供高精度定时器.你可以使用API函数QueryPerformanceFre ...

  6. 关于java调用linux shell 的问题

    问题的提出: shell脚本要做离线的数据处理任务 java调用脚本,将这种处理任务封装成webservice 特点: shell处理单个时间长 每次要处理文件量大 这里目前只做调用分析: 原来的: ...

  7. unity3d 自动保存

    using UnityEngine; using UnityEditor; using System; public class AutoSave : EditorWindow { private b ...

  8. 解决Notice错误,性能竟然提升了1000多倍!

    先说PHP的deprecated错误的性能问题 最近刚刚完成了一个项目,在测试完基本功能后,我们就发布到线上.结果上线不久就发现产生了大量的错误,如下图: 一看都是PHP的Deprecated错误,是 ...

  9. C#&java重学笔记(函数)

    C#部分  1.写在函数定义的()中的关键字: a.params关键字:用来满足函数的参数为数组时,而数组的长度不固定的情况.且该关键字只能用来修饰数组型参数.这样一修饰,就达成了类似JavaScri ...

  10. editplus的各式插件

    C/C++, Java, JSP, C#, .NET, SQL, Pascal, Python, Assembly, Basic files http://www.editplus.com/javac ...