Road Construction
King Mercer is the king of ACM kingdom. There are one capital and some cities in his kingdom. Amazingly, there are no roads in the kingdom now. Recently, he planned to construct roads between the capital and the cities, but it turned out that the construction cost of his plan is much higher than expected.
In order to reduce the cost, he has decided to create a new construction plan by removing some roads from the original plan. However, he believes that a new plan should satisfy the following conditions:
- For every pair of cities, there is a route (a set of roads) connecting them.
- The minimum distance between the capital and each city does not change from his original plan.
Many plans may meet the conditions above, but King Mercer wants to know the plan with minimum cost. Your task is to write a program which reads his original plan and calculates the cost of a new plan with the minimum cost.
Input
The input consists of several datasets. Each dataset is formatted as follows.
N M
u1 v1 d1 c1
.
.
.
uM vM dM cM
The first line of each dataset begins with two integers, N and M (1 ≤ N ≤ 10000, 0 ≤ M ≤ 20000). N and M indicate the number of cities and the number of roads in the original plan, respectively.
The following M lines describe the road information in the original plan. The i-th line contains four integers, ui, vi, di and ci (1 ≤ ui, vi ≤ N , ui ≠ vi , 1 ≤ di ≤ 1000, 1 ≤ ci ≤ 1000). ui , vi, di and ci indicate that there is a road which connects ui-th city and vi-th city, whose length is di and whose cost needed for construction is ci.
Each road is bidirectional. No two roads connect the same pair of cities. The 1-st city is the capital in the kingdom.
The end of the input is indicated by a line containing two zeros separated by a space. You should not process the line as a dataset.
Output
For each dataset, print the minimum cost of a plan which satisfies the conditions in a line.
Sample Input
3 3
1 2 1 2
2 3 2 1
3 1 3 2
5 5
1 2 2 2
2 3 1 1
1 4 1 1
4 5 1 1
5 3 1 1
5 10
1 2 32 10
1 3 43 43
1 4 12 52
1 5 84 23
2 3 58 42
2 4 86 99
2 5 57 83
3 4 11 32
3 5 75 21
4 5 23 43
5 10
1 2 1 53
1 3 1 65
1 4 1 24
1 5 1 76
2 3 1 19
2 4 1 46
2 5 1 25
3 4 1 13
3 5 1 65
4 5 1 34
0 0
Output for the Sample Input
3
5
137
218 题解:
首先,我把模型抽象出来,就是求一棵单源最短路树,使得这棵树的权值最小。
真是,我把这个模型抽象出来之后就马上有想法了,但是wa了,现在还是不知道有什么问题,就是先跑spfa,然后将dis[now]=dis[to]+quan的边扣出来,单独跑一遍最小生成树,但wa了,我现在还不知道为什么。
然后换一下思路,对于dis相同的点,我们只要跑spfa时记录一笑dis最小时,以他为去处的最小花费就可以了。
代码:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <iostream>
#include <queue>
#define MAXN 100000
using namespace std;
struct edge{
int first;
int next;
int to;
int quan1,quan2;
}a[MAXN*];
int dis[MAXN],have[MAXN],node[MAXN];
queue<int> q;
int n,m,num,k,ans;
void cl(){
memset(a,,sizeof(a));
num=ans=;
} void addedge(int from,int to,int quan1,int quan2){
a[++num].to=to;
a[num].quan1=quan1,a[num].quan2=quan2;
a[num].next=a[from].first;
a[from].first=num;
} void spfa(){
memset(dis,,sizeof(dis));
memset(node,,sizeof(node));
memset(have,,sizeof(have));
dis[]=node[]=,have[]=;
q.push();
while(!q.empty()){
int now=q.front();
q.pop();
have[now]=;
for(int i=a[now].first;i;i=a[i].next){
int to=a[i].to,quan=a[i].quan1,quan2=a[i].quan2;
if(dis[to]>dis[now]+quan){
dis[to]=dis[now]+quan,node[to]=quan2;
if(!have[to]){
have[to]=;
q.push(to);
}
}
else if(dis[to]==dis[now]+quan&&node[to]>quan2) node[to]=quan2;
}
}
} int main()
{
while(){
scanf("%d%d",&n,&m);
if(!n&&!m) break;
cl();
for(int i=;i<=m;i++){
int x,y,z,d;
scanf("%d%d%d%d",&x,&y,&z,&d);
addedge(x,y,z,d),addedge(y,x,z,d);
}
spfa();
for(int i=;i<=n;i++) ans+=node[i];
printf("%d\n",ans);
}
return ;
}
Road Construction的更多相关文章
- POJ3352 Road Construction(边双连通分量)
...
- POJ3352 Road Construction (双连通分量)
Road Construction Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- poj 3352 Road Construction【边双连通求最少加多少条边使图双连通&&缩点】
Road Construction Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10141 Accepted: 503 ...
- POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)
POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的.一份代码能交.给定一个连通无向图,问加几条边能使得图变成一个双连通图 ...
- POJ3352 Road Construction 双连通分量+缩点
Road Construction Description It's almost summer time, and that means that it's almost summer constr ...
- 【Tarjan缩点】PO3352 Road Construction
Road Construction Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12532 Accepted: 630 ...
- POJ P3352 Road Construction 解题报告
P3352 Road Construction 描述 这几乎是夏季,这意味着它几乎是夏季施工时间!今年,负责岛屿热带岛屿天堂道路的优秀人士,希望修复和升级岛上各个旅游景点之间的各种道路. 道路本身也很 ...
- [POJ3352]Road Construction
[POJ3352]Road Construction 试题描述 It's almost summer time, and that means that it's almost summer cons ...
- POJ-3352 Road Construction,tarjan缩点求边双连通!
Road Construction 本来不想做这个题,下午总结的时候发现自己花了一周的时间学连通图却连什么是边双连通不清楚,于是百度了一下相关内容,原来就是一个点到另一个至少有两条不同的路. 题意:给 ...
- 【Aizu - 2249】Road Construction(最短路 Dijkstra算法)
Road Construction Descriptions Mercer国王是ACM王国的王者.他的王国里有一个首都和一些城市.令人惊讶的是,现在王国没有道路.最近,他计划在首都和城市之间修建道路, ...
随机推荐
- 深度剖析Vue中父给子、子给父、兄弟之间传值!
本片文章将为您详细讲解在Vue中,父给子传值.子给父传值以及兄弟之间传值方式! 父传子:父组件 // template里面 <aa :info="name"/> // ...
- 每天学会一点点(map常量)
map常用的声明方式(使用静态代码块): public final static Map map = new HashMap(); static { map.put("key1", ...
- vue项目集成金格WebOffice2015
下载 官网地址:http://www.goldgrid.com/jinge_download/index.aspx?num=5 解压后的文件 js文件中有两个重要的js文件iWebOffice2015 ...
- 作为一名Android开发者,你有过迷茫吗?
前言 经常听新进的小白问道,Android是不是饱和了?想写一篇关于Android开发者忧虑的文章很久了,今天才提起勇气写.最近不管是在微信.QQ群,还是在各大博客网站,都随处听得到Android开发 ...
- Net基础篇_学习笔记_第十天_方法(函数)
方法(函数): 函数就是将一堆代码进行重用的一种机制.//解决冗余代码问题------方法出现了. 面向对象的三大特征:封装.继承.多态 函数的语法:[public] stati ...
- .Net基础篇_学习笔记_第七天_Continue关键字的用法
Continue: 立即结束本次循环,判断循环条件: 如果成立,则进行下一次循环,否则退出循环. Continue和break的区别: 遇到break,循环不继续. 遇到continue,本次循环也不 ...
- 使用java程序作为celery的工作节点
celery是python实现的分布式调度框架,有时候想用celery去调用java服务,正好有一个celery-java的库可以使用,能达到这个效果,记录一下: 先添加依赖: <depende ...
- soap get/post请求
pom.xml依赖: <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactI ...
- mybatis动态拼接条件的技巧 where 1=1 或者where标签
/** * 根据输入的学生信息进行条件检索 * 1. 当只输入用户名时, 使用用户名进行模糊检索: * 2. 当只输入邮箱时, 使用性别进行完全匹配 * 3. 当用户名 ...
- JavaEE就业学习路线(给初学者以及自学者一个学习方向)
大家按这个路线学完后基本可以找工作了 第一节java入门 1-Java 背景介绍 2-Java 入门程序的编写 3-环境配置 4-基本概念介绍 5-类型转换 6-开发工具使用 第二节java基础 1- ...