Dhaka city is getting crowded and noisy day by day. Certain roads always remain blocked in congestion. In order to convince people avoid shortest routes, and hence the crowded roads, to reach destination, the city authority has made a new plan. Each junction of the city is marked with a positive integer (≤ 20) denoting the busyness of the junction. Whenever someone goes from one junction (the source junction) to another (the destination junction), the city authority gets the amount (busyness of destination – busyness of source)3 (that means the cube of the difference) from the traveler. The authority has appointed you to find out the minimum total amount that can be earned when someone intelligent goes from a certain junction (the zero point) to several others.
Input
Input starts with an integer T (≤ 50), denoting the number of test cases.
Each case contains a blank line and an integer n (1 < n ≤ 200) denoting the number of junctions. The next line contains nintegers denoting the busyness of the junctions from 1 to n respectively. The next line contains an integer m, the number of roads in the city. Each of the next m lines (one for each road) contains two junction-numbers (source, destination) that the corresponding road connects (all roads are unidirectional). The next line contains the integer q, the number of queries. The next q lines each contain a destination junction-number. There can be at most one direct road from a junction to another junction.
Output
For each case, print the case number in a single line. Then print q lines, one for each query, each containing the minimum total earning when one travels from junction 1 (the zero point) to the given junction. However, for the queries that gives total earning less than 3, or if the destination is not reachable from the zero point, then print a ‘?’.
Sample Input
2
5
6 7 8 9 10
6
1 2
2 3
3 4
1 5
5 4
4 5
2
4
5
2
10 10
1
1 2
1
2
Sample Output
Case 1:
3
4
Case 2:
?
题意:给n个点以及点的喧闹值,给m条边u·v,边权等于(w[v]-w[u])^3,给一系列询问q,给出q点离1的距离,如果d[q]<3或者不能到达点1或者有负环,则GG。
题解:SPFA判环,可以选择记录所有点的入队次数,也可以找到一个点后就把相连所有点全打上标记,一般来说第二种快。
代码1:
#include<algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <cassert> #include <cstdio> #include <vector> #include <string> #include <cmath> #include <queue> #include <stack> #include <set> #include <map> using namespace std; #define P(init,b,c) make_pair(init,make_pair(b,c)) #define rep(i,init,n) for (int i=init;i<=n;i++) #define per(i,init,n) for (int i=n;i>=init;i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef pair<int,pair<int,int> >pii; typedef long long ll; const ll mod = 1000000007; ll gcd(ll init, ll b) { return b ? gcd(b, init%b) : init; } const int MAXN=1e5+5; const int INF=0x3f3f3f3f; struct Edge { int v; int cost; Edge(int _v=0,int _cost=0):v(_v),cost(_cost){} }; vector<Edge>E[MAXN]; void addedge(int u,int v,int w) { E[u].push_back(Edge(v,w)); } bool vis[MAXN];//在队列标志 int cnt[MAXN];//每个点的入队列次数 int dist[MAXN]; void SPFA(int start,int n) { memset(vis,false,sizeof(vis)); for(int i=1;i<=n;i++) dist[i]=INF; vis[start]=true; dist[start]=0; queue<int>que; while(!que.empty()) que.pop(); que.push(start); memset(cnt,0,sizeof(cnt)); cnt[start]=1; while(!que.empty()) { int u=que.front(); que.pop(); vis[u]=false; for(int i=0;i<E[u].size();i++) { int v=E[u][i].v; if(dist[v]>dist[u]+E[u][i].cost) { dist[v]=dist[u]+E[u][i].cost; if(!vis[v]&&++cnt[v]<=n) { vis[v]=true; que.push(v); } } } } } int w[MAXN]; int main(){ int t; scanf("%d", &t); rep(i,1,t){ printf("Case %d:\n",i); int n; scanf("%d", &n); rep(i,1,n)E[i].clear(); rep(j,1,n){ scanf("%d", &w[j]); } int m; scanf("%d", &m); int u,v; rep(j,1,m){ scanf("%d%d", &u,&v); addedge(u,v,(w[v]-w[u])*(w[v]-w[u])*(w[v]-w[u])); } SPFA(1,n); int q; scanf("%d", &q); while(q--){ int s; scanf("%d", &s); if(dist[s]<3||dist[s]==INF||cnt[s]>n){ printf("?\n"); }else printf("%d\n", dist[s]); } } return 0; }
代码2:(kuangbin)
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <time.h> using namespace std; const int MAXN = 220; const int INF = 0x3f3f3f3f; struct Edge { int v,cost; Edge(int _v = 0, int _cost = 0) { v = _v; cost = _cost; } }; vector<Edge>E[MAXN]; void addedge(int u,int v,int w) { E[u].push_back(Edge(v,w)); } bool vis[MAXN]; int cnt[MAXN]; int dist[MAXN]; bool cir[MAXN]; void dfs(int u) { cir[u] = true; for(int i = 0;i < E[u].size();i++) if(!cir[E[u][i].v]) dfs(E[u][i].v); } void SPFA(int start,int n) { memset(vis,false,sizeof(vis)); for(int i = 1;i <= n;i++) dist[i] = INF; vis[start] = true; dist[start] = 0; queue<int>que; while(!que.empty())que.pop(); que.push(start); memset(cnt,0,sizeof(cnt)); cnt[start] = 1; memset(cir,false,sizeof(cir)); while(!que.empty()) { int u = que.front(); que.pop(); vis[u] = false; for(int i = 0;i < E[u].size();i++) { int v = E[u][i].v; if(cir[v])continue; if(dist[v] > dist[u] + E[u][i].cost) { dist[v] = dist[u] + E[u][i].cost; if(!vis[v]) { vis[v] = true; que.push(v); cnt[v]++; if(cnt[v] > n) dfs(v); } } } } } int a[MAXN]; int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int T; int iCase = 0; scanf("%d",&T); while(T--) { iCase++; int n; scanf("%d",&n); for(int i = 1;i <= n;i++) scanf("%d",&a[i]); int m; int u,v; for(int i = 1;i <= n;i++) E[i].clear(); scanf("%d",&m); while(m--) { scanf("%d%d",&u,&v); addedge(u,v,(a[v]-a[u])*(a[v]-a[u])*(a[v]-a[u])); } SPFA(1,n); printf("Case %d:\n",iCase); scanf("%d",&m); while(m--) { scanf("%d",&u); if(cir[u] || dist[u] < 3 || dist[u] == INF) printf("?\n"); else printf("%d\n",dist[u]); } } return 0; }