Language: C++

#include"stdafx.h"
#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<queue>
#include<string>
#include<cstdio>
#include<stack>
#include<memory.h>
#include<cmath>
#include<bitset>
#include<functional>
#include<memory>
#include<utility>
using namespace std;
#define FOR(i,m,n) for(i=m;i<n;i++)
#define MIN(x,y) (x>y?y:x)
#define ab(x,y) (x>y ?(x-y):(y-x))
#define CLR(a,b) memset(a,b,sizeof(a))
#define ROF(i,m,n) for(i=m;i>=n;i--)
#define BUG cout<<"debug"<<endl;
#define RT return
#define BK break
#define HEAP priority_queue
#define SP " "
#define lowf(a,b,c) lower_bound(a,b,c)
#define upf(a,b,c) upper_bound(a,b,c)
#define CP bool cmp(node A,node B)
#define maxn 100000
typedef long long LL;
typedef vector<int> VI;
const int INF = 2147483647;
const int NEN = -INF - 1;
const LL LINF = 9223372036854775807;
typedef struct edge
{
	int dest;
	int cost;
	int dis;
	edge(int de, int c, int di)
	{
		dest = de; c = cost; dis = di;
	}
	edge()
	{
		;
	}
}edge;
typedef struct bie
{
	int fro, too;
	int cost, dis;
	bie()
	{
		;
	}
	bie(int a,int b,int c,int d)
	{
		fro = a; too = b; cost = c; dis = d;
	}
}bie;
bool cmp(bie A, bie B)
{
	return A.cost > B.cost;
}
vector<vector<edge>>Map(10010);
bie all[20010];
bool vis[10005];
int N, M;
typedef struct node
{
	int key, far;
	node()
	{
		far = INF;
	}
	node(int k, int f)
	{
		key = k;
		far = f;
	}
	friend bool operator <(node A, node B)
	{
		return A.far > B.far;
	}
}node;
int fax[10005];
int rax[10005];
priority_queue<node>DJ;
bool dj()
{
	int i = 0;
	while (!DJ.empty())DJ.pop();
	FOR(i, 0, N + 2)fax[i] = INF;
	CLR(vis, 0);
	DJ.push(node(1, 0));
	fax[1] = 0;
	vis[1] = 1;
	int sum = 1;
	int tempk, tempf;
	while (!DJ.empty())
	{
		tempk = DJ.top().key;
		tempf = DJ.top().far;
		DJ.pop();
		if (!vis[tempk])
		{
			vis[tempk] = 1;
			fax[tempk] = tempf;
			if (rax[tempk] < fax[tempk])
			{
				return false;
			}
			sum++;
		}
		if (sum == N)return true;
		for (i = 0; i < Map[tempk].size(); i++)
		{
			if (vis[Map[tempk][i].dest] == 0)DJ.push(node(Map[tempk][i].dest, Map[tempk][i].dis + fax[tempk]));
		}
	}
	if (sum < N)
	{
		RT	false;
	}
	return true;
}
int main()
{
	while (cin >> N >> M&&N != 0 && M != 0)
	{
		int u, v, d, c, i, j, I = 0;
		LL ans = 0;
		FOR(i, 0, N + 2)
		{
			fax[i] = INF;
			rax[i] = INF;
		}
		FOR(i, 0, M)
		{
			//cin >> u >> v >> d >> c;
			scanf("%d %d %d %d", &u, &v, &d, &c);
			ans += c;
			Map[u].push_back(edge(v, c, d));
			Map[v].push_back(edge(u, c, d));
			all[I++] = bie(u, v, c, d);
		}
		sort(all, all + I, cmp);
		int st = 0;
		dj();
		FOR(i, 0, N + 2)rax[i] = fax[i];
		bie temp;
		int a, b;
		FOR(st, 0, I)
		{
			temp = all[st];
			a = temp.fro; b = temp.too;
			for (j = 0; j < Map[a].size(); j++)
			{
				if (Map[a][j].dest == b)
				{
					Map[a].erase(Map[a].begin() + j);
					break;
				}
			}
			for (j = 0; j < Map[b].size(); j++)
			{
				if (Map[b][j].dest == a)
				{
					Map[b].erase(Map[b].begin() + j);
					break;
				}
			}
			if (dj() == true)ans -= temp.cost;
			else
			{
				Map[a].push_back(edge(b, temp.cost, temp.dis));
				Map[b].push_back(edge(a, temp.cost, temp.dis));
			}
		}
		cout << ans << endl;
		FOR(i, 0, N + 2)Map[i].clear();
	}
}


Compiler output:
a.cc:1:9: fatal error: stdafx.h: No such file or directory
    1 | #include"stdafx.h"
      |         ^~~~~~~~~~
compilation terminated.