stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

struct node{

    int next,d,c;

};

const int maxn=10005;

int n,m;

int d[maxn],c[maxn];

bool vis[maxn];

void init()

{

    for(int i=0;i<maxn;i++)

        d[i]=c[i]=1e9,vis[i]=0;

}

int main()

{

    while(~scanf("%d%d",&n,&m))

    {

        if(n==0&&m==0) return 0;

        init();vector<node> v[maxn];

        for(int i=0;i<m;i++)

        {

            int x,y,d,c;

            scanf("%d%d%d%d",&x,&y,&d,&c);

            v[x].push_back(node{y,d,c});

            v[y].push_back(node{x,d,c});

        }

        queue<int> q;

        q.push(1);

        d[1]=c[1]=0;vis[1]=1;

        while(!q.empty())

        {

            int u=q.front();q.pop();vis[u]=0;

            for(int i=0;i<v[u].size();i++)

            {

                int nx=v[u][i].next;

                if(d[nx]==d[u]+v[u][i].d&&c[nx]>v[u][i].c){

                        c[nx]=v[u][i].c;

                }

                if(d[nx]>d[u]+v[u][i].d)

                {

                    d[nx]=d[u]+v[u][i].d;

                    c[nx]=v[u][i].c;

                    if(vis[nx]) continue;

                    vis[nx]=1;

                    q.push(nx);

                }

            }

        }

        int ans=0;

        for(int i=2;i<=n;i++) ans+=c[i];

        cout<<ans<<endl;

    }

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.2607088001758036x):
#include <cstdio>

#include <queue>

#include <vector>

#include <algorithm>



using namespace std;

const int INF = 0x3f3f3f3f;



const int maxn = 10240;



struct edge {

	int to, dis, cost;

	edge(int t, int d, int c) : to(t), dis(d), cost(c) {}

};





typedef pair<int, int> p;

priority_queue<p, vector<p>, greater<p> > q;

vector<edge> g[maxn];



int N, M;



void read_in() {

	//scanf("%d%d", &N, &M);

	for(int i = 1; i <= N; i++)

		g[i].clear();



	int from, to, dis, cost;

	for(int i = 1; i <= M; i++) {

		scanf("%d%d%d%d", &from, &to, &dis, &cost);

		g[from].push_back(edge(to, dis, cost));

		g[to].push_back(edge(from, dis, cost));

	}

}



int solve() {

	q.push(p(0, 1));



	const int INF = 0x7777777;

	int d[maxn];



	for(int i = 2; i <= N; i++) 

		d[i] = INF;

	d[1] = 0;



	while( !q.empty() ) {

		p t = q.top();

		q.pop();



		int to = t.second, dis = t.first;

        if(d[to] < dis) continue;

		for(unsigned int i = 0; i < g[to].size(); i++) {

			edge To = g[to][i];

			if(d[To.to] > d[to] + To.dis)

				d[To.to] = d[to] + To.dis,

				q.push(p(d[To.to], To.to));

		}

	}

	//show(d);

    int ans = 0;



    for(int i = 2; i <= N; i++) {

        int tmp = INF;



        for(int j = 0; j < g[i].size(); j++) {

            edge e = g[i][j];

            if(d[e.to] + e.dis == d[i] and e.cost < tmp)

                tmp = e.cost;

        }

        ans += tmp;

    }



    return ans;

}



int main() {

	while(scanf("%d %d", &N, &M) and N) {

		read_in();

		printf("%d\n", solve());

	}

	return 0;

}
// Slower C++ solution:
#include<iostream>

#include<cstdio>

#include<cstdlib>

#include<string>

#include<cstring>

#include<algorithm>

#include<queue>

#include<vector>

#define N 20100

#define M 50010

#define rg register

using namespace std;

typedef long long ll;

typedef double db;

int n,m,tot=0;

int dis[N],head[N],vis[N];

int nxt[M],to[M],w[M],c[M];

void link(int x,int y,int z,int v){

	nxt[tot]=head[x],to[tot]=y,w[tot]=z,c[tot]=v,head[x]=tot++;

}

void spfa(){

	queue <int> q;

	memset(dis,0x3f,sizeof(dis));

	memset(vis,0,sizeof(vis));

	q.push(1),dis[1]=0,vis[1]=1;

	while (!q.empty()){

		int x=q.front();q.pop();

		for (int i=head[x];i!=-1;i=nxt[i]){

			int v=to[i];

			if (dis[v]<=dis[x]+w[i]) continue;

			dis[v]=dis[x]+w[i];

			if (!vis[v]) vis[v]=1,q.push(v);

		}

		vis[x]=0;

	}

}

int main(){

//	freopen ("2249.in","r",stdin);

//	freopen ("2249.out","w",stdout);

	while (scanf ("%d%d",&n,&m)&&(n||m)){

		memset(head,-1,sizeof(head));

		tot=0;int x,y,z,v;

		for (int i=1;i<=m;++i){

			scanf ("%d%d%d%d",&x,&y,&z,&v);

			link(x,y,z,v),link(y,x,z,v);

		}

		spfa();

		int ans=0;

		for (int i=2;i<=n;++i){

			int mx=2000;

			for (int j=head[i];j!=-1;j=nxt[j]){

				v=to[j];

				if (dis[i]==dis[v]+w[j]) mx=min(mx,c[j]);

			}

			ans+=mx;

		}

		printf ("%d\n",ans);

	

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.1088845676786745x):
#include <cstdio>

#include <queue>

#include <vector>

#include <algorithm>



using namespace std;

const int INF = 0x3f3f3f3f;



const int maxn = 10240;



struct edge {

	int to, dis, cost;

	edge(int t, int d, int c) : to(t), dis(d), cost(c) {}

};





typedef pair<int, int> p;

priority_queue<p, vector<p>, greater<p> > q;

vector<edge> g[maxn];



int N, M;



void read_in() {

	//scanf("%d%d", &N, &M);

	for(int i = 1; i <= N; i++)

		g[i].clear();



	int from, to, dis, cost;

	for(int i = 1; i <= M; i++) {

		scanf("%d%d%d%d", &from, &to, &dis, &cost);

		g[from].push_back(edge(to, dis, cost));

		g[to].push_back(edge(from, dis, cost));

	}

}



int solve() {

	q.push(p(0, 1));



	const int INF = 0x7777777;

	int d[maxn];



	for(int i = 2; i <= N; i++) 

		d[i] = INF;

	d[1] = 0;



	while( !q.empty() ) {

		p t = q.top();

		q.pop();



		int to = t.second, dis = t.first;

        if(d[to] < dis) continue;

		for(unsigned int i = 0; i < g[to].size(); i++) {

			edge To = g[to][i];

			if(d[To.to] > d[to] + To.dis)

				d[To.to] = d[to] + To.dis,

				q.push(p(d[To.to], To.to));

		}

	}

	//show(d);

    int ans = 0;



    for(int i = 2; i <= N; i++) {

        int tmp = INF;



        for(int j = 0; j < g[i].size(); j++) {

            edge e = g[i][j];

            if(d[e.to] + e.dis == d[i] and e.cost < tmp)

                tmp = e.cost;

        }

        ans += tmp;

    }



    return ans;

}



int main() {

	while(scanf("%d %d", &N, &M) and N) {

		read_in();

		printf("%d\n", solve());

	}

	return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;

const int N = 1E4 + 7;

bool vis[N];

int n, m;

typedef struct node

{

   int to,dis,cost;

   node(){}

   node(int a,int b,int c) : to(a),dis(b),cost(c) {}

   bool operator < (const node &a) const {

       return dis == a.dis ? cost > a.cost : dis > a.dis;

   }

}P;

vector<P>g[N];

int dij()

{

    int ans = 0;

    priority_queue<P> pq;

    pq.push(P(1,0,0));

    while(!pq.empty()) {

        P now = pq.top() ; pq.pop();

        if(vis[now.to]) continue;

        vis[now.to] = true;

        ans += now.cost;

        for(int i = 0;i < g[now.to].size();i ++) {

            P temp = g[now.to][i];

            pq.push(P(temp.to, now.dis + temp.dis, temp.cost));

        }

    }

    return ans;

}

int main()

{

   // freopen("f:\\jj.txt","r",stdin);

    std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);

    while(cin >> n >> m) {

        if(n + m == 0)  break;

        for(int i = 1;i <= n;i ++)  g[i].clear();

        memset(vis, 0, sizeof(vis));

        for(int i = 1;i <= m;i ++) {

            int a,b,c,d;

            cin >> a >> b >> c >> d;

            g[a].push_back(P(b,c,d));

            g[b].push_back(P(a,c,d));

        }

        cout << dij() << endl;

    }

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.112417880843806x):
#include <cstdio>

#include <cstring>

#include <algorithm>

#include <queue>



using namespace std;



const int inf = 0x3f3f3f3f;

const int N = 10414;

const int M = 40414;



struct Edge{

	int u, v, d, c;

	int next;

}edge[M];



int cnt, head[N];

int op, tp, link[N];

int vis[N], dis[N], cost[N];

queue<int>q;



void add_edge(int u, int v, int d, int c);

void spfa(int n);



int main(){

	int n, m, i, u, v, d, c;

	while(scanf("%d %d", &n, &m) && (n || m)){

		cnt = 0;

		memset(head, -1, sizeof(head));

		for(i = 0; i < m; i++){

			scanf("%d %d %d %d", &u, &v, &d, &c);

			add_edge(u, v, d, c);

			add_edge(v, u, d, c);

		}

		spfa(n);

	}

	return 0;

}

void add_edge(int u, int v, int d, int c){

	edge[cnt].u = u;

	edge[cnt].v = v;

	edge[cnt].d = d;

	edge[cnt].c = c;

	edge[cnt].next = head[u];

	head[u] = cnt++;

}

void spfa(int n)

{

    int v, d, c;

    while(!q.empty())q.pop();

    memset(vis, 0,sizeof(vis));

    memset(dis, inf, sizeof(dis));

    memset(cost, inf, sizeof(cost));

    dis[1]=0;

    cost[1]=0;

    vis[1]=true;

    q.push(1);

    while(!q.empty())

    {

        int u=q.front();

        q.pop();

        vis[u]=false;

        for(int i=head[u]; i!=-1; i=edge[i].next)

        {

            v = edge[i].v;

            d = edge[i].d;

            c = edge[i].c;

            if(dis[v] > dis[u] + d||(dis[v] == dis[u] + d && cost[v] > c))//有多条最短路时，取花费最小的

            {

                dis[v] = dis[u] +d;

                cost[v] = c;

                if(!vis[v])

                {

                    q.push(v);

                    vis[v]=true;

                }

            }

        }

    }

    int sum = 0;

    for(int i = 1; i <= n; i++){

    	sum += cost[i];

    }

    printf("%d\n", sum);

    return;

}
// Slower C++ solution:
#include "iostream"

#include "cstring"

#include "cstdio"

#include "queue"

using namespace std;

const int N = 10010;

const int INF = 0x3fffffff;

int pre[N];

struct Edge

{

	int x, y, d, c;

	int next;

	Edge(){}

	Edge(int X, int Y, int D, int C):x(X), y(Y), d(D), c(C){}

}p, q, e[N * 4];

int dis[N], cost[N], vis[N]; 

queue<int> que;

void spfa(int n)

{

	for(int i = 1; i <= n; ++ i){

		dis[i] = INF;

		vis[i] = 0;

	}

	dis[1] = 0;

	cost[1] = 0;

	que.push(1);

	while(!que.empty()){

		int u = que.front();

		vis[u] = 0;

		que.pop();

		for(int i = pre[u]; i != -1; i = e[i].next){

			int v = e[i].y, d = e[i].d, c = e[i].c;

			if(dis[v] > dis[u] + d || (dis[v] == dis[u] + d && cost[v] > c)){

				dis[v] = dis[u] + d;

				cost[v] = c;

				if(!vis[v]){

					vis[v] = 1;

					que.push(v);

				}

			}

		}

	}

} 

int main(void)

{

	int n, m;

	while(scanf("%d %d", &n, &m) && (n || m)){

		memset(pre, -1, sizeof(pre));

		int x, y, d, c;

		int tot = 1;

		for(int i = 1; i <= m; ++ i){

			scanf("%d %d %d %d", &x, &y, &d, &c);

			e[tot] = Edge(x, y, d, c);

			e[tot].next = pre[x];

			pre[x] = tot++;

			e[tot] = Edge(y, x, d, c);

			e[tot].next = pre[y];

			pre[y] = tot++;

		}

		spfa(n);

		int ans = 0;

		for(int i = 1; i <= n; ++ i){

			ans += cost[i];

		}

		cout << ans << endl;

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 2.5162539123791907x):
#include <cstdio>

#include <cstring>

#include <algorithm>



using namespace std;



const int inf = 0x3f3f3f3f;

const int N = 10414;

const int M = 40414;



struct Edge{

	int u, v, d, c;

	int next;

}edge[M];



int cnt, head[N];

int vis[N], dis[N], cost[N];

int op, tp, link[18014];



void add_edge(int u, int v, int d, int c);

void spfa(int n);



int main(){

	int n, m, i, u, v, d, c;

	while(~scanf("%d %d", &n, &m) && (n || m)){

		cnt = 0;

		memset(head, -1, sizeof(head));

		for(i = 0; i < m; i++){

			scanf("%d %d %d %d", &u, &v, &d, &c);

			add_edge(u, v, d, c);

			add_edge(v, u, d, c);

		}

		spfa(n);

	}

	return 0;

}

void add_edge(int u, int v, int d, int c){

	edge[cnt].u = u;

	edge[cnt].v = v;

	edge[cnt].d = d;

	edge[cnt].c = c;

	edge[cnt].next = head[u];

	head[u] = cnt++;

	return;

}

void spfa(int n){

	int i, u, v, d, c, sum;

	memset(vis, 0, sizeof(vis));

	memset(dis, inf, sizeof(dis));

	memset(cost, inf, sizeof(cost));

	op = tp = 0;

	vis[1] = 1, dis[1] = 0, cost[1] = 0;

	link[tp++] = 1;

	while(op < tp){

		u = link[op++];

		vis[u] = 0;

		for(i = head[u]; ~i; i = edge[i].next){

			v = edge[i].v, d = edge[i].d, c = edge[i].c;



			if(dis[v] > dis[u] + d || (dis[v] == dis[u] + d && cost[v] > c)){

				dis[v] = dis[u] + d;

				cost[v] = c;

				if(!vis[v]){

					vis[v] = 1;

					link[tp++] = v;

				}

			}

		}

	}

	sum = 0;

	for(i = 1; i <= n; i++){

		sum += cost[i];

	}

	printf("%d\n", sum);

	return;

}
// Slower C++ solution:
#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <queue>

#include <algorithm>

using namespace std;

#define MAXN 10010

#define MAXM 40010



int n,m;

struct node

{

    int v,d,c,next;

}e[MAXM];

int ecnt,pre[MAXN],dis[MAXN],co[MAXN];

bool vis[MAXN];

void add(int u,int v,int d,int c)

{

    e[ecnt].v=v;

    e[ecnt].d=d;

    e[ecnt].c=c;

    e[ecnt].next=pre[u];

    pre[u]=ecnt++;

}

void spfa()

{

    queue<int> q;

    q.push(1);

    memset(vis,0,sizeof(vis));

    memset(dis,0x3f,sizeof(dis));

    memset(co,0x3f,sizeof(co));

    dis[1]=co[1]=0;

    while (!q.empty())

    {

        int u=q.front();

        q.pop();

        vis[u]=0;

        for (int i=pre[u];i!=-1;i=e[i].next)

        {

            int v=e[i].v;

            if ((dis[v]>dis[u]+e[i].d)||(dis[v]==dis[u]+e[i].d&&co[v]>e[i].c))

            {

                dis[v]=dis[u]+e[i].d;

                co[v]=e[i].c;

                if (!vis[v])

                {

                    vis[v]=1;

                    q.push(v);

                }

            }

        }

    }

}

int main()

{

    //freopen("/home/moor/Code/input","r",stdin);

    while (scanf("%d%d",&n,&m)&&n+m)

    {

        ecnt=0;

        memset(pre,-1,sizeof(pre));

        int u,v,d,c;

        for (int i=0;i<m;i++)

        {

            scanf("%d%d%d%d",&u,&v,&d,&c);

            add(u,v,d,c);

            add(v,u,d,c);

        }

        spfa();

        int ans=0;

        for (int i=2;i<=n;i++)

            ans+=co[i];

        printf("%d\n",ans);

    }

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 2.6040787764925333x):
#include <cstdio>

#include <cstring>

#include <algorithm>



using namespace std;



const int inf = 0x3f3f3f3f;

const int N = 10414;

const int M = 40414;



struct Edge{

	int u, v, d, c;

	int next;

}edge[M];



int cnt, head[N];

int vis[N], dis[N], cost[N];

int op, tp, link[18014];



void add_edge(int u, int v, int d, int c);

void spfa(int n);



int main(){

	int n, m, i, u, v, d, c;

	while(~scanf("%d %d", &n, &m) && (n || m)){

		cnt = 0;

		memset(head, -1, sizeof(head));

		for(i = 0; i < m; i++){

			scanf("%d %d %d %d", &u, &v, &d, &c);

			add_edge(u, v, d, c);

			add_edge(v, u, d, c);

		}

		spfa(n);

	}

	return 0;

}

void add_edge(int u, int v, int d, int c){

	edge[cnt].u = u;

	edge[cnt].v = v;

	edge[cnt].d = d;

	edge[cnt].c = c;

	edge[cnt].next = head[u];

	head[u] = cnt++;

	return;

}

void spfa(int n){

	int i, u, v, d, c, sum;

	memset(vis, 0, sizeof(vis));

	memset(dis, inf, sizeof(dis));

	memset(cost, inf, sizeof(cost));

	op = tp = 0;

	vis[1] = 1, dis[1] = 0, cost[1] = 0;

	link[tp++] = 1;

	while(op < tp){

		u = link[op++];

		vis[u] = 0;

		for(i = head[u]; ~i; i = edge[i].next){

			v = edge[i].v, d = edge[i].d, c = edge[i].c;



			if(dis[v] > dis[u] + d || (dis[v] == dis[u] + d && cost[v] > c)){

				dis[v] = dis[u] + d;

				cost[v] = c;

				if(!vis[v]){

					vis[v] = 1;

					link[tp++] = v;

				}

			}

		}

	}

	sum = 0;

	for(i = 1; i <= n; i++){

		sum += cost[i];

	}

	printf("%d\n", sum);

	return;

}
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;

typedef pair<int, int> P;

typedef long long ll;



const int maxv = 10000 + 10;

struct edge

{

    int to, d, c;

    edge() {}

    edge(int to, int d, int c):to(to), d(d), c(c) {}

};

vector<edge> G[maxv];

int d[maxv];



int N, M;



void dijkstra(int s)

{

    priority_queue<P, vector<P>, greater<P> > que;

    memset(d, 0x3f, sizeof(d));

    d[s] = 0;

    que.push(P(0, s));

    while(!que.empty())

    {

        P p = que.top(); que.pop();

        int v = p.second;

        if(p.first > d[v]) continue;

        for(int i=0; i<G[v].size(); i++)

        {

            edge e = G[v][i];

            if(d[e.to] > d[v]+e.d)

            {

                d[e.to] = d[v] + e.d;

                que.push(P(d[e.to], e.to));

            }

        }

    }

}



int main()

{

    while(scanf("%d%d", &N, &M)==2 && N)

    {

        for(int i=0; i<N; i++)  G[i].clear();

        for(int i=0; i<M; i++)

        {

            int u, v, d, c;

            scanf("%d%d%d%d", &u, &v, &d, &c);

            u--; v--;

            G[u].push_back(edge(v, d, c));

            G[v].push_back(edge(u, d, c));

        }

        dijkstra(0);

        int ans = 0;

        for(int i=1; i<N; i++)

        {

            int mincost = 0x3f3f3f3f;

            for(int j=0; j<G[i].size(); j++)

            {

                edge e = G[i][j];

                if(d[e.to]+e.d==d[i] && e.c<mincost)

                    mincost = e.c;

            }

            ans += mincost;

        }

        printf("%d\n", ans);

    }

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 3.0974935011912015x):
#include <cstdio>

#include <cstring>

#include <algorithm>



using namespace std;



const int inf = 0x3f3f3f3f;

const int N = 10414;

const int M = 40414;



struct Edge{

	int u, v, d, c;

	int next;

}edge[M];



int cnt, head[N];

int vis[N], dis[N], cost[N];

int op, tp, link[18014];



void add_edge(int u, int v, int d, int c);

void spfa(int n);



int main(){

	int n, m, i, u, v, d, c;

	while(~scanf("%d %d", &n, &m) && (n || m)){

		cnt = 0;

		memset(head, -1, sizeof(head));

		for(i = 0; i < m; i++){

			scanf("%d %d %d %d", &u, &v, &d, &c);

			add_edge(u, v, d, c);

			add_edge(v, u, d, c);

		}

		spfa(n);

	}

	return 0;

}

void add_edge(int u, int v, int d, int c){

	edge[cnt].u = u;

	edge[cnt].v = v;

	edge[cnt].d = d;

	edge[cnt].c = c;

	edge[cnt].next = head[u];

	head[u] = cnt++;

	return;

}

void spfa(int n){

	int i, u, v, d, c, sum;

	memset(vis, 0, sizeof(vis));

	memset(dis, inf, sizeof(dis));

	memset(cost, inf, sizeof(cost));

	op = tp = 0;

	vis[1] = 1, dis[1] = 0, cost[1] = 0;

	link[tp++] = 1;

	while(op < tp){

		u = link[op++];

		vis[u] = 0;

		for(i = head[u]; ~i; i = edge[i].next){

			v = edge[i].v, d = edge[i].d, c = edge[i].c;



			if(dis[v] > dis[u] + d || (dis[v] == dis[u] + d && cost[v] > c)){

				dis[v] = dis[u] + d;

				cost[v] = c;

				if(!vis[v]){

					vis[v] = 1;

					link[tp++] = v;

				}

			}

		}

	}

	sum = 0;

	for(i = 1; i <= n; i++){

		sum += cost[i];

	}

	printf("%d\n", sum);

	return;

}
// Slower C++ solution:
#include <queue>

#include <vector>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <iostream>

#include <algorithm>

const int INF=0x3f3f3f3f;

using namespace std;

struct edge{

    int to,cost,w;

};

int V,d[10005],dis[10005];

vector<edge> G[20005];

typedef pair<int,int> P;

void dijkstra(int s){

    int i,v;

	priority_queue<P,vector<P>,greater<P> >que;

	fill(d,d+V+1,INF);

	fill(dis,dis+V+1,INF);

	d[s]=dis[s]=0;

	que.push(P(0,s));

	while(!que.empty()){

		P p=que.top(); que.pop();

		v=p.second;

		if(d[v]<p.first)

        continue;

		for(i=0;i<G[v].size();i++){

			edge e=G[v][i];

			if(d[e.to]>d[v]+e.cost){

				d[e.to]=d[v]+e.cost;

                dis[e.to]=e.w;

				que.push(P(d[e.to],e.to));

			}

			else if(d[e.to]==d[v]+e.cost)

            dis[e.to]=min(dis[e.to],e.w);

		}

	}

}

int main(){

    int m,st,en,i,j,x,y,z,q,ans;

    edge temp;

    while(scanf("%d%d",&V,&m)!=EOF&&(V||m)){

        for(i=0;i<=V;i++)

        G[i].clear();

        for(i=0;i<m;i++){

            scanf("%d%d%d%d",&x,&y,&z,&q);

            temp.to=y;temp.cost=z;temp.w=q;

            G[x].push_back(temp);

            temp.to=x;

            G[y].push_back(temp);

        }

        dijkstra(1);

        ans=0;

        for(i=1;i<=V;i++)

        ans+=dis[i];

        printf("%d\n",ans);

    }

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 3.5523102426189483x):
#include <cstdio>

#include <cstring>

#include <algorithm>



using namespace std;



const int inf = 0x3f3f3f3f;

const int N = 10414;

const int M = 40414;



struct Edge{

	int u, v, d, c;

	int next;

}edge[M];



int cnt, head[N];

int vis[N], dis[N], cost[N];

int op, tp, link[18014];



void add_edge(int u, int v, int d, int c);

void spfa(int n);



int main(){

	int n, m, i, u, v, d, c;

	while(~scanf("%d %d", &n, &m) && (n || m)){

		cnt = 0;

		memset(head, -1, sizeof(head));

		for(i = 0; i < m; i++){

			scanf("%d %d %d %d", &u, &v, &d, &c);

			add_edge(u, v, d, c);

			add_edge(v, u, d, c);

		}

		spfa(n);

	}

	return 0;

}

void add_edge(int u, int v, int d, int c){

	edge[cnt].u = u;

	edge[cnt].v = v;

	edge[cnt].d = d;

	edge[cnt].c = c;

	edge[cnt].next = head[u];

	head[u] = cnt++;

	return;

}

void spfa(int n){

	int i, u, v, d, c, sum;

	memset(vis, 0, sizeof(vis));

	memset(dis, inf, sizeof(dis));

	memset(cost, inf, sizeof(cost));

	op = tp = 0;

	vis[1] = 1, dis[1] = 0, cost[1] = 0;

	link[tp++] = 1;

	while(op < tp){

		u = link[op++];

		vis[u] = 0;

		for(i = head[u]; ~i; i = edge[i].next){

			v = edge[i].v, d = edge[i].d, c = edge[i].c;



			if(dis[v] > dis[u] + d || (dis[v] == dis[u] + d && cost[v] > c)){

				dis[v] = dis[u] + d;

				cost[v] = c;

				if(!vis[v]){

					vis[v] = 1;

					link[tp++] = v;

				}

			}

		}

	}

	sum = 0;

	for(i = 1; i <= n; i++){

		sum += cost[i];

	}

	printf("%d\n", sum);

	return;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;



typedef long long ll;

const int N = 10005;

const int M = 1000000007;



int n, m;

typedef int wtype;

struct Edge { int v; wtype d, c; };

vector<Edge> e[N];

wtype dist[N], cost[N];

int pre[N];

bool vis[N];

int SPFA(int src) {

  memset(dist, 0x3f, sizeof(dist)); dist[src] = 0;

  memset(cost, 0x3f, sizeof(cost)); cost[src] = 0;

  memset(vis, 0, sizeof(vis)); vis[src] = true;

  stack<int> que; que.push(src);

  while (!que.empty()) {

    int u = que.top(); que.pop();

    vis[u] = false;

    for (int i = 0; i < (int)e[u].size(); i++) {

      int v = e[u][i].v; wtype d = e[u][i].d, c = e[u][i].c;

      if (dist[v] > dist[u] + d || (dist[v] == dist[u] + d && cost[v] != 0x3f3f3f3f && cost[v] > c)) {

        dist[v] = dist[u] + d; cost[v] = c;

        if (!vis[v]) { vis[v] = true; que.push(v); }

      }

    }

  }

  int ret = 0;

  for (int i = 1; i <= n; i++) { ret += cost[i]; }

  return ret;

}



int main() {

  while (scanf("%d%d", &n, &m), n) {

    for (int i = 1; i <= n; i++) { e[i].clear(); }

    for (int i = 0, u, v, d, c; i < m; i++) {

      scanf("%d%d%d%d", &u, &v, &d, &c);

      e[u].push_back((Edge) {v, d, c});

      e[v].push_back((Edge) {u, d, c});

    }

    printf("%d\n", SPFA(1));

  }

}

// Faster C++ solution for the same problem (measured speedup: 3.2501494002390605x):
#include <cstdio>

#include <cstring>

#include <algorithm>



using namespace std;



const int inf = 0x3f3f3f3f;

const int N = 10414;

const int M = 40414;



struct Edge{

	int u, v, d, c;

	int next;

}edge[M];



int cnt, head[N];

int vis[N], dis[N], cost[N];

int op, tp, link[18014];



void add_edge(int u, int v, int d, int c);

void spfa(int n);



int main(){

	int n, m, i, u, v, d, c;

	while(~scanf("%d %d", &n, &m) && (n || m)){

		cnt = 0;

		memset(head, -1, sizeof(head));

		for(i = 0; i < m; i++){

			scanf("%d %d %d %d", &u, &v, &d, &c);

			add_edge(u, v, d, c);

			add_edge(v, u, d, c);

		}

		spfa(n);

	}

	return 0;

}

void add_edge(int u, int v, int d, int c){

	edge[cnt].u = u;

	edge[cnt].v = v;

	edge[cnt].d = d;

	edge[cnt].c = c;

	edge[cnt].next = head[u];

	head[u] = cnt++;

	return;

}

void spfa(int n){

	int i, u, v, d, c, sum;

	memset(vis, 0, sizeof(vis));

	memset(dis, inf, sizeof(dis));

	memset(cost, inf, sizeof(cost));

	op = tp = 0;

	vis[1] = 1, dis[1] = 0, cost[1] = 0;

	link[tp++] = 1;

	while(op < tp){

		u = link[op++];

		vis[u] = 0;

		for(i = head[u]; ~i; i = edge[i].next){

			v = edge[i].v, d = edge[i].d, c = edge[i].c;



			if(dis[v] > dis[u] + d || (dis[v] == dis[u] + d && cost[v] > c)){

				dis[v] = dis[u] + d;

				cost[v] = c;

				if(!vis[v]){

					vis[v] = 1;

					link[tp++] = v;

				}

			}

		}

	}

	sum = 0;

	for(i = 1; i <= n; i++){

		sum += cost[i];

	}

	printf("%d\n", sum);

	return;

}