2 条题解
-
1xuyifeng LV 10 MOD @ 2017-08-25 19:09:42
------------------------------------------------AC code------------------------------------------------
#include<cstring> #include<cstdio> #include<queue> using namespace std; const int INF = 0x7fffffff; const int MAXN = 100005; const int MAXM = 300005; int n, m, k, u, v, p, spe[MAXN], S[MAXN], dexs, T[MAXN], dext, ans = INF; bool b[MAXN]; inline int read(){ int x = 0; bool fl = 1; char ch = getchar(); while(ch < '0' && ch > '9'){ if(ch == '-') fl = 0; ch = getchar(); } while(ch >= '0' && ch <= '9'){ x = x * 10 + ch - '0'; ch = getchar(); } return fl ? x : -x; } struct Node{ int dis, num; bool operator < (const Node& a) const{ return a.dis < dis; } }; struct Edge{ int nxt, to, dis; }edge[MAXM]; int head[MAXN], edge_num; void add_edge(int from, int to, int dis){ edge[++edge_num].nxt = head[from]; edge[edge_num].to = to; edge[edge_num].dis = dis; head[from] = edge_num; } int dis[MAXN]; bool vis[MAXN]; void dijkstra(){ priority_queue<Node> q; for(int i = 1; i <= n; i++) dis[i] = INF, vis[i] = 0; for(int i = 1; i <= dexs; i++){ q.push((Node){0, S[i]}); dis[S[i]] = 0; } while(!q.empty()){ Node x = q.top(); q.pop(); int cur = x.num; if(vis[cur]) continue; vis[cur] = 1; for(int i = head[cur]; i; i = edge[i].nxt){ if(dis[cur] != INF && dis[edge[i].to] > dis[cur] + edge[i].dis){ dis[edge[i].to] = dis[cur] + edge[i].dis; q.push((Node){dis[edge[i].to], edge[i].to}); } } } } int main(){ //freopen("Graph.in", "r", stdin); //freopen("Graph.out", "w", stdout); n = read(), m = read(), k = read(); for(int i = 1; i <= m; i++){ u = read(), v = read(), p = read(); add_edge(u, v, p); } for(int i = 1; i <= k; i++) spe[i] = read(); int a = 16; while(a!=-1){ memset(S, 0, sizeof S); dexs = 0; memset(b, 0, sizeof b); for(int i = 1; i <= k; i++){ int temp = (spe[i]>>a) & 1; if(temp) S[++dexs] = spe[i]; else b[spe[i]] = 1; } dijkstra(); for(int i = 1; i <= n; i++) if(b[i]) ans = min(ans, dis[i]); memset(S, 0, sizeof S); dexs = 0; memset(T, 0, sizeof T); dext = 0; memset(b, 0, sizeof b); for(int i = 1; i <= k; i++){ int temp = (spe[i]>>a) & 1; if(temp) T[++dext] = spe[i], b[spe[i]] = 1; else S[++dexs] = spe[i]; } dijkstra(); for(int i = 1; i <= n; i++) if(b[i]) ans = min(ans, dis[i]); a--; } printf("%d", ans); return 0; }
-
02017-08-25 19:10:42@
---------------------------------------------Extra Test TLE---------------------------------------------
#include<algorithm> #include<cstdlib> #include<cstring> #include<cstdio> #include<queue> using namespace std; const int INF = 0x7fffffff; const int MAXN = 100005; const int MAXM = 300005; int n, m, k, u, v, p, spe[MAXN], S[MAXN], dexs, T[MAXN], dext, ans = INF; bool b[MAXN]; inline int read(){ int x = 0; bool fl = 1; char ch = getchar(); while(ch < '0' && ch > '9'){ if(ch == '-') fl = 0; ch = getchar(); } while(ch >= '0' && ch <= '9'){ x = x * 10 + ch - '0'; ch = getchar(); } return fl ? x : -x; } struct Edge{ int nxt, to, dis; }edge[MAXM]; int head[MAXN], edge_num; inline void add_edge(int from, int to, int dis){ edge[++edge_num].nxt = head[from]; edge[edge_num].to = to; edge[edge_num].dis = dis; head[from] = edge_num; } int dis[MAXN]; bool inq[MAXN]; inline void spfa(){ for(int i = 1; i <= n; i++) dis[i] = INF; queue<int> q; for(int i = 1; i <= dexs; i++){ q.push(S[i]); inq[S[i]] = 1; dis[S[i]] = 0; } while(!q.empty()){ int cur = q.front(); q.pop(); inq[cur] = 0; for(int i = head[cur]; i; i = edge[i].nxt){ if(dis[cur] < INF && dis[edge[i].to] > dis[cur] + edge[i].dis){ dis[edge[i].to] = dis[cur] + edge[i].dis; if(!inq[edge[i].to]){ q.push(edge[i].to); inq[edge[i].to] = 1; } } } } } int main(){ //freopen("Graph.in", "r", stdin); //freopen("Graph.out", "w", stdout); srand(423186579); n = read(), m = read(), k = read(); for(int i = 1; i <= m; i++){ u = read(), v = read(), p = read(); add_edge(u, v, p); } for(int i = 1; i <= k; i++) spe[i] = read(); random_shuffle(spe+1, spe+k+1); int a = 16; while(a!=-1){ memset(S, 0, sizeof S); dexs = 0; memset(T, 0, sizeof T); dext = 0; memset(b, 0, sizeof b); for(int i = 1; i <= k; i++){ int temp = (spe[i]>>a) & 1; if(temp) S[++dexs] = spe[i]; else T[++dext] = spe[i], b[spe[i]] = 1; } spfa(); for(int i = 1; i <= n; i++) if(b[i]) ans = min(ans, dis[i]); memset(S, 0, sizeof S); dexs = 0; memset(T, 0, sizeof T); dext = 0; memset(b, 0, sizeof b); for(int i = 1; i <= k; i++){ int temp = (spe[i]>>a) & 1; if(temp) T[++dext] = spe[i], b[spe[i]] = 1; else S[++dexs] = spe[i]; } spfa(); for(int i = 1; i <= n; i++) if(b[i]) ans = min(ans, dis[i]); a--; } printf("%d", ans); return 0; }
- 1