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中TextField中显示图标

    现看实现效果:   实现代码: { width:500, height : 25, fieldLabel : '过滤条件', itemId : 'queryText', labelAlign : 'r ...

  2. 发送Http Get和Post请求

    发送Get请求 HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Method = "GET"; r ...

  3. bzoj 3489: A simple rmq problem k-d树思想大暴力

    3489: A simple rmq problem Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 551  Solved: 170[Submit][ ...

  4. 阻止浏览器关闭 区分刷新和关闭 自试IE可用

    window.onbeforeunload = onbeforeunload_handler; function onbeforeunload_handler(){ if(event.clientX& ...

  5. asp.net 分布式缓存

    之前Velocity已被 集成到App Fabric(包含有WCF监控==)中.   网络Velocity使用大多是针对老版本:  老版本的下载地址:  http://www.microsoft.co ...

  6. 一道题DP

    Problem Description 小明明又被大威鱼抓住了,大威鱼把小明明关在地牢里,地牢由n * n 个房间组成,小明被困在地牢的最左上角的房间中,出口在最右下角,他想逃出这个诡异的地牢,但是他 ...

  7. Chapter 4 持久存储数据对象

    1.使用with open("filename.扩展名","r/w/rb/wb") as data代替data=open(..);data.close() 打开 ...

  8. Subclasses

    Given a collection of numbers, return all possible subclasses. public class Solution { public List&l ...

  9. cf 383 D

    D. Antimatter time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  10. 有趣 GIF 动图集 - 仿佛每张小动图都诉说了一个小笑话或者小故事

    点这里 来自法国南特(Nantes)的 Guillaume Kurkdjian 目前还是个学生.Kurkdjian 擅长创作一些平面动态图像,这些有趣的小动图仿佛每张都诉说了一个小笑话或者小故事,像个 ...