Extended Traffic LightOJ - 1074

题目链接:https://vjudge.net/problem/LightOJ-1074

思路:(busyness of destination - busyness of source)3 可能会是负值,那么可能出现负环,

需要SFPA来解决,我们需要把负环中的点能到达其他点都标记为‘?’点,因为这些点在有限次循环后

会变成负值,一定小于3,可以用dfs来遍历能经过的所有点。


  1 #include 
  2 #include 
  3 #include 
  4 #include 
  5 #include 
  6 #include 
  7 #include <string>
  8 #include 
  9 #include 
 10 #include 
 11 using namespace std;
 12  
 13 typedef long long LL;
 14 #define inf 1e9
 15 #define rep(i,j,k) for(int i = (j); i <= (k); i++)
 16 #define rep__(i,j,k) for(int i = (j); i < (k); i++)
 17 #define per(i,j,k) for(int i = (j); i >= (k); i--)
 18 #define per__(i,j,k) for(int i = (j); i > (k); i--)
 19 
 20 const int N = 210;
 21 int head[N];
 22 int tot[N];
 23 bool vis[N];
 24 int tmp[N];
 25 bool bad[N];
 26 int dis[N];
 27 stack<int> sta;
 28 int cnt;
 29 int bus[N];
 30 int n,m;
 31 
 32 struct Edge{
 33     int to;
 34     int w;
 35     int next;
 36 }e[N * N];
 37 
 38 void add(int u,int v,int w){
 39     e[cnt].to = v;
 40     e[cnt].w = w;
 41     e[cnt].next = head[u];
 42     head[u] = cnt++;
 43 }
 44 //遍历所有能到达的点
 45 void dfs(int u){
 46 
 47     bad[u] = true;
 48 
 49     for(int o = head[u]; ~o; o = e[o].next){
 50         if(!bad[e[o].to]) 
 51             dfs(e[o].to);    
 52     }
 53 }
 54 
 55 void SPFA(){
 56 
 57     while(!sta.empty()) sta.pop();
 58     rep(i,1,n) vis[i] = false;
 59     rep(i,1,n) bad[i] = false;
 60     rep(i,1,n) dis[i] = inf;
 61     rep(i,1,n) tot[i] = 0;
 62     dis[1] = 0;
 63     sta.push(1);
 64 
 65     int u,v,w;
 66     while(!sta.empty()){
 67         u = sta.top();
 68         sta.pop();
 69         if(bad[u]) continue; //‘?’点
 70         vis[u] = false;
 71 
 72         for(int o = head[u]; ~o; o = e[o].next){
 73             w = e[o].w;
 74             v = e[o].to;
 75 
 76             if(!bad[v]/* '?'点 */ && dis[u] + w < dis[v]){
 77                 dis[v] = dis[u] + w;
 78                 // printf("this is dis[%d] = %d\n",v,dis[v]);
 79                 if(!vis[v]){
 80                     if(++tot[v] > n) dfs(v);
 81                     else{
 82                         vis[v] = true;
 83                         sta.push(v);
 84                     }
 85                 }
 86             }
 87         }
 88     }
 89 }
 90 
 91 void print(int o){
 92 
 93     int tot;
 94     scanf("%d",&tot);
 95     rep(i,1,tot){
 96         scanf("%d",&tmp[i]);
 97     }
 98     printf("Case %d:\n",o);
 99     rep(i,1,tot){
100         if(bad[tmp[i]] || dis[tmp[i]] < 3 || dis[tmp[i]] == inf) printf("?\n");
101         else printf("%d\n",dis[tmp[i]]);
102     }
103 }
104 
105 int main(){
106 
107     int T;
108     scanf("%d",&T);
109 
110     int u,v;
111     rep(o,1,T){
112         scanf("%d",&n);
113         rep(i,1,n){
114             scanf("%d",&bus[i]);
115         }
116 
117         rep(i,1,n) head[i] = -1;
118         cnt = 0;
119 
120         scanf("%d",&m);
121         rep(i,1,m){
122             scanf("%d%d",&u,&v);
123             add(u,v,(bus[v] - bus[u])*(bus[v] - bus[u])*(bus[v] - bus[u]));
124         }
125 
126         SPFA();
127         print(o);
128     }
129 
130     getchar(); getchar();
131     return 0;
132 }

 

你可能感兴趣的:(Extended Traffic LightOJ - 1074)