比赛 |
平凡的题目 |
评测结果 |
ATTTA |
题目名称 |
平凡的皮卡丘 |
最终得分 |
40 |
用户昵称 |
璞瑞 |
运行时间 |
3.015 s |
代码语言 |
C++ |
内存使用 |
7.97 MiB |
提交时间 |
2015-11-03 11:12:37 |
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
typedef long long int i64;
template<class T> inline bool mint(T& a, T b) { return a > b ? a = b, true : false; }
template<class T> inline bool maxt(T& a, T b) { return a < b ? a = b, true : false; }
const int maxn = 100333;
#ifdef WINVER
#define QAQ "%I64d"
#else
#define QAQ "%lld"
#endif
struct ed
{
int v, w; ed * nx; ed() {}
ed(int _v, int _w, ed * _nx)
: v(_v), w(_w), nx(_nx) {}
} *G[maxn], mem[maxn << 2], *all = mem;
int n, m, vis[maxn << 2];
struct dij
{
i64 dis; int o;
inline bool operator<(const dij& other)
const { return dis > other.dis; }
}; priority_queue<dij> Q;
i64 dist[maxn];
inline i64 mindist(int s, int fbd)
{
static int vvv[maxn]; int cnt = 0;
Q.push((dij) { dist[s] = 0, s });
while (Q.size())
{
int o = Q.top().o; Q.pop();
if (vis[o]) continue;
vis[vvv[cnt++] = o] = true;
for (ed *p = G[o]; p; p = p->nx) if ((p - mem) >>1 != fbd)
if (!vis[p->v] && mint(dist[p->v], dist[o] + p->w))
Q.push((dij) { dist[p->v], p->v });
}
i64 ret = dist[1];
while (cnt --> 0)
{
dist[vvv[cnt]] = 0x3f3f3f3f3f3f3f3fLL;
vis[vvv[cnt]] = 0;
} return ret;
}
int main()
{
freopen("both.in", "r", stdin);
#ifndef debug
freopen("both.out", "w", stdout);
#endif
scanf("%d%d", &n, &m);
for (int i = 0; i < m; ++i)
{
int u, v, x, y; scanf("%d%d%d%d", &u, &v, &x, &y);
G[u] = new(all++) ed(v, x, G[u]);
G[v] = new(all++) ed(u, y, G[v]);
}
memset(dist, 0x3f, sizeof(dist));
i64 ans = 0x3f3f3f3f3f3f3f3fLL;
for (ed *p = G[1]; p; p = p->nx)
mint(ans, mindist(p->v, (p - mem) >> 1) + p->w);
if (ans == 0x3f3f3f3f3f3f3f3fLL) puts("-1");
else printf(QAQ"\n", ans);
}