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, uividi and ci (1 ≤ uivi ≤ N , ui ≠ vi , 1 ≤ di ≤ 1000, 1 ≤ ci ≤ 1000). ui , vidi 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的更多相关文章

  1. POJ3352 Road Construction(边双连通分量)

                                                                                                         ...

  2. POJ3352 Road Construction (双连通分量)

    Road Construction Time Limit:2000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u Sub ...

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

    Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10141   Accepted: 503 ...

  4. POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)

    POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的.一份代码能交.给定一个连通无向图,问加几条边能使得图变成一个双连通图 ...

  5. POJ3352 Road Construction 双连通分量+缩点

    Road Construction Description It's almost summer time, and that means that it's almost summer constr ...

  6. 【Tarjan缩点】PO3352 Road Construction

    Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12532   Accepted: 630 ...

  7. POJ P3352 Road Construction 解题报告

    P3352 Road Construction 描述 这几乎是夏季,这意味着它几乎是夏季施工时间!今年,负责岛屿热带岛屿天堂道路的优秀人士,希望修复和升级岛上各个旅游景点之间的各种道路. 道路本身也很 ...

  8. [POJ3352]Road Construction

    [POJ3352]Road Construction 试题描述 It's almost summer time, and that means that it's almost summer cons ...

  9. POJ-3352 Road Construction,tarjan缩点求边双连通!

    Road Construction 本来不想做这个题,下午总结的时候发现自己花了一周的时间学连通图却连什么是边双连通不清楚,于是百度了一下相关内容,原来就是一个点到另一个至少有两条不同的路. 题意:给 ...

  10. 【Aizu - 2249】Road Construction(最短路 Dijkstra算法)

    Road Construction Descriptions Mercer国王是ACM王国的王者.他的王国里有一个首都和一些城市.令人惊讶的是,现在王国没有道路.最近,他计划在首都和城市之间修建道路, ...

随机推荐

  1. 试试 IEnumerable 的另外 6 个小例子

    IEnumerable 接口是 C# 开发过程中非常重要的接口,对于其特性和用法的了解是十分必要的.本文将通过6个小例子,来熟悉一下其简单的用法. <!-- more --> 阅读建议 在 ...

  2. pycharm中报ImportError: libcublas.so.9.0错误的解决方法。

    前些天不知为啥cuda不能用了,nvidia-smi也没反应.然后我就重新装了一下cuda.后来使用pycharm远程连接时,居然报错了. ImportError: libcublas.so.9.0: ...

  3. springboot postman 对象里传时间格式问题

    主要问题是系列化的问题,在定义时间变量处使用如下的注解即可.导包注意了······ 如果springmvc使用的是com.fasterxml.jackson 的jar包则直接使用一下注解即可 impo ...

  4. charles 远程映射到URL地址

    本文参考:charles 映射到远程URL地址 远程映射/Map Remote Settings 功能:把你要请求的地址,映射到一个远程地址:相当于把你的请求地址修改了: 远程映射工具 远程映射工具根 ...

  5. 个人微信Hook-C#Demo开发SDK

    目录 基础信息类 好友操作 群操作类 发送信息 接收信息 Demo源码 基础信息类 获取个人信息 从网络获取群成员信息 从网络获取个人信息 获取公众号信息 获取群组信息 获取群成员信息 获取单个好友信 ...

  6. Google、百度搜索引擎高级应用6/15

    不得不知道的搜索技巧.

  7. pt-table-sync 配合pt-table-checksum 修复主从不一致

    pt-table-sync 配合pt-table-checksum 修复主从不一致. 先执行下面这条语句,打印出要执行的命令,确认无误后再将  --print  改为  --execute 执行.注意 ...

  8. php将图片存储在阿里云oss存储上

    创建两个方法 1.上传方法 use OSS\OssClient; use think\Config; use OSS\Core\OssException; /** * 存储文件 * * @param ...

  9. 品Spring:帝国的基石

    序 生活是一杯酒,有时需要麻醉自己,才能够暂时忘却痛苦与不快.生活是一杯茶,有时需要细细品味,才发现苦涩背后也会有甘甜. Spring是一杯酒,一眼望不到边的官方文档,着实让人难以下咽.Spring是 ...

  10. Python连载40-协程定义及状态、send语句、yield用法

    一.协程 1.历史进程: (1)3.4引入协程,用yield来实现 (2)3.5引入协程语法 (3)实现协程比较好的包有asyncio,tornado,gevent 2.定义:协程是为非抢占式多任务产 ...