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 ;
}

最新文章

  1. C# where用法
  2. 【转】C# 使用消息队列,包括远程访问
  3. ajax返回数据解析总结
  4. [原]一个简单的Linux TCP Client所涉及到的头文件
  5. 前后台使用ajax传list的时候,用value[] 获取值
  6. MySql 插入10位以上长度的字符报错or截断
  7. HTTPS=HTTP + SSL / TLS
  8. JAVA_SE基础——66.StringBuffer类 ③
  9. 大数据学习总结(7)we should...
  10. 如何在shell脚本中判断文件或者文件夹是否存在?
  11. Java工程师修炼之路(校招总结)
  12. 将数据转换成树型层级的Json格式的String
  13. utils工具类
  14. TCP的三次握手与四次释放
  15. Windows 2008 Server搭建Radius服务器的方法
  16. Flink简介及使用
  17. 直接在apk中添加资源的研究
  18. EF Core 2.0中Transaction事务会对DbContext底层创建和关闭数据库连接的行为有所影响
  19. request获取路径方式
  20. 【转载】Unity3D研究院之IOS&amp;Andoird使用Sharesdk遇到的坑

热门文章

  1. Java并发编程之ThreadLocal解析
  2. 解开Batch Normalization的神秘面纱
  3. 【LeetCode】406-根据身高重建队列
  4. Django-下载安装-配置-创建django项目-三板斧简单使用
  5. Go语言基础之基本数据类型
  6. 使用eclipse Debug时总是被URLClassLoader这个类拦截,不能进入到要调试的类里面去
  7. Oracle 实用SQL
  8. HttpServlet类
  9. CSS——样式表的引入
  10. PopupWindow弹出框