AOJ-2249 Road Construction(最短路)

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

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

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

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

输出最小花费。

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

 1 #include <iostream>
 2 #include <vector>
 3 #include <queue>
 4 #include <cstdio>
 5 using namespace std;
 6
 7 const int maxn = 10010;
 8 const int INF = 1<<29;
 9 struct edge {
10     int to,cost,distance;
11     edge(){}
12     edge( int x,int y,int z ) {
13         to=x;
14         distance=y;
15         cost=z;
16     }
17 };
18
19 typedef pair<int,int>P;
20 vector<edge>G[maxn];
21 int d[maxn];
22 int N;
23
24 void dijkstra(int s) {
25     priority_queue<P,vector<P>,greater<P> >que;
26     for(int i=1;i<=N;i++) d[i]=INF;
27     d[s]=0;
28     que.push(P(0,s));
29
30     while(!que.empty()) {
31         P p=que.top(); que.pop();
32         int v=p.second;
33         if(d[v]<p.first) continue;
34         for(int i=0;i<G[v].size();i++) {
35             edge e=G[v][i];
36             if(d[e.to]>d[v]+e.distance) {
37                 d[e.to]=d[v]+e.distance;
38                 que.push(P(d[e.to],e.to));
39             }
40         }
41     }
42 }
43
44 int main()
45 {
46    //freopen("a.txt","r",stdin);
47     int M;
48     while(~scanf("%d%d",&N,&M)&&(N+M)) {
49         for(int i=1;i<=N;i++) G[i].clear();
50         int a,b,c,v;
51         for(int i=0;i<M;i++) {
52             scanf("%d%d%d%d",&a,&b,&c,&v);
53           //  printf("%d %d %d %d\n",a,b,c,v);
54             G[a].push_back(edge(b,c,v));
55             G[b].push_back(edge(a,c,v));
56         }
57         dijkstra(1);
58        // for(int i=1;i<=N;i++) printf("%d\n",d[i]);
59         int sum=0;
60         for(int i=2;i<=N;++i) {
61             int min_cost=INF;
62             for(int j=0;j<G[i].size();++j) {
63                 edge &e=G[i][j];
64                // printf("%d %d %d\n",e.to,d[e.to],e.distance);
65                 if(d[e.to]+e.distance==d[i]&&e.cost<min_cost)
66                 {
67                     min_cost=e.cost;
68                 }
69             }
70             sum+=min_cost;
71         }
72         printf("%d\n",sum);
73     }
74     return 0;
75 }
时间: 2024-07-28 14:28:12

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

AOJ 2249 Road Construction (dijkstra)

某国王需要修路,王国有一个首都和多个城市,需要修路.已经有修路计划了,但是修路费用太高. 为了减少修路费用,国王决定从计划中去掉一些路,但是需要满足一下两点: 保证所有城市都能连通 所有城市到首都的最短路不变 思路: 在Dijkstra找最短路的时候,就记录一下费用 if(d[e.to] > d[v] + e.dist) { ... prev_min_cost[e.to] = e.cost; // 最短路必经之路,则费用也必须要 } else if(d[e.to] == d[v] + e.dis

Aizu - 2249 Road Construction

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

杭电 1596 find the safest road (最短路)

http://acm.hdu.edu.cn/showproblem.php?pid=1596 这道题目与杭电2544最短路的思想是一样的,只不过是把+改成了*,输入输出有些不一样而已. find the safest road Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6985    Accepted Submission(s)

POJ 3352 Road Construction(图论-tarjan)

Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8647   Accepted: 4318 Description It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of the ro

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

Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10141   Accepted: 5031 Description It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of the r

POJ 3352 Road Construction 使得无向图边变双连通图

点击打开链接 Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8168   Accepted: 4106 Description It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of

POJ 题目 Road Construction(双连通)

Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9353   Accepted: 4648 Description It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of the ro

POJ 3177 Redundant Paths POJ 3352 Road Construction(双连通)

POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的,一份代码能交,给定一个连通无向图,问加几条边能使得图变成一个双连通图 思路:先求双连通,缩点后,计算入度为1的个数,然后(个数 + 1) / 2 就是答案(这题由于是只有一个连通块所以可以这么搞,如果有多个,就不能这样搞了) 代码: #include <cstdio> #include <cstring> #include <algorithm&

Road Construction POJ - 3352 (边双连通分量)

Road Construction POJ - 3352 题意:一个无向图(无重边),问至少还要加多少边使得去掉任意一条边后任意两点仍可互达. 无向图的边双连通分量(无重边) 先用一次dfs标记出割边,然后dfs标记出各联通分量 再根据割边,缩点重新建图,生成一颗树 则答案就是(叶子树+1)/2. 1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring