LightOJ1074 Extended Traffic【SPFA+DFS 標記負環的結點】
Extended Traffic
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個點,每個點有權值ai,從i到j的邊權為:(aj−ai)^3。問從1到達k的最短路,不能到達和路徑長小於3輸出? 其它輸出長度
解題思路:可能出現負環,使用SPFA演算法,出現負環時,負環可達的結點都應該輸出?,入隊次數超過n次,標記它是負環中的結點,從此節點進行DFS,它所能達的結點的最短路徑也為負無窮
AC的C++程式:
#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
const int N=205;
const int INF=0x3f3f3f3f;
int dist[N],b[N];
int g[N][N];
bool vis[N];
bool f[N];//標記結點是否在負環中
int cnt[N];
int n;
void dfs(int u)
{
f[u]=true;
for(int v=1;v<=n;v++)
if(!f[v]&&v!=u&&g[u][v]!=INF)
dfs(v);
}
void spfa(int s)
{
memset(dist,INF,sizeof(dist));
memset(vis,false,sizeof(vis));
memset(f,false,sizeof(f));
memset(cnt,0,sizeof(cnt));
queue<int>q;
vis[s]=true;
cnt[s]=1;
dist[s]=0;
q.push(s);
while(!q.empty()){
int u=q.front();
q.pop();
vis[u]=false;
if(!f[u]){//如果u不是負環中的點
for(int v=1;v<=n;v++)
if(!f[v]&&v!=u&&g[u][v]!=INF&&dist[v]>dist[u]+g[u][v]){
dist[v]=dist[u]+g[u][v];
if(!vis[v]){
vis[v]=true;
cnt[v]++;
if(cnt[v]>n)//v是負環中的點
dfs(v);//標記v可達的點也是負環中的點
else
q.push(v);
}
}
}
}
}
int main()
{
int T,m,u,v,q,x;
scanf("%d",&T);
for(int t=1;t<=T;t++){
scanf("%d",&n);
memset(g,INF,sizeof(g));
for(int i=1;i<=n;i++)
scanf("%d",&b[i]);
scanf("%d",&m);
while(m--){
scanf("%d%d",&u,&v);
g[u][v]=(b[v]-b[u])*(b[v]-b[u])*(b[v]-b[u]);
}
spfa(1);
scanf("%d",&q);
printf("Case %d:\n",t);
while(q--){
scanf("%d",&x);
if(f[x]||dist[x]==INF||dist[x]<3)
printf("?\n");
else
printf("%d\n",dist[x]);
}
}
return 0;
}
相關文章
- Scheduling and Traffic Shaping 學習筆記(一)API筆記
- 連結串列中環的入口結點
- 給每一個結點新增向右的next指標結點指標
- JZ-055-連結串列中環的入口結點
- Seattle Traffic construction projects punlicationStructProject
- 判斷負環模板
- html標記與文件結構HTML
- label studio 結合 MMDetection 實現資料集自動標記、模型迭代訓練的閉環模型
- Java : 常用重點正則標記Java
- Extended Window Manager Hints(1)
- leetcode 876. 連結串列的中間結點(快慢指標法)LeetCode指標
- Oracle 21C Extended ClustersOracle
- LTE-5G學習筆記5---Normal CP和Extended CP的區別筆記ORM
- CAD標記、刪除標記、放大標記、縮小標記
- BZOJ4773: 負環(倍增Floyd)
- 開環和閉環;正反饋和負反饋
- 網速監控軟體 Traffic Monitor
- java環形連結串列約瑟夫環問題筆記Java筆記
- Azure Traffic Manager(二) 基於權重與基於優先順序的路由策略為我們的Web專案提供負載均衡路由Web負載
- Redis知識點筆記總結Redis筆記
- 筆記:React 中關於 key 的一點總結筆記React
- 剖析網路測量:Counting and Measuring Network Traffic
- 判斷單連結串列中是否存在環,並輸出環入口節點。
- [20210223]sys與Extended Data Types.txt
- 關於VUE專案地圖開發中大量點標記繪製一些總結Vue地圖
- 知識點:樹中結點的度以及葉子結點(度為0的結點)的計算
- 【連結串列問題】打卡8:複製含有隨機指標節點的連結串列隨機指標
- Linux環境下Nginx及負載均衡LinuxNginx負載
- POJ 3259-Wormholes(簡單判負環)Worm
- Redis筆記 — 連結串列和連結串列節點的API函式(三)Redis筆記API函式
- 剖析HBase負載均衡和效能指標負載指標
- 【Dataset】Maple-IDS - Network Security Malicious Traffic Detection Dataset
- (18)企業採購招標系統之Ribbon結合RestTemplate實現負載均衡REST負載
- link/Extended dependency 無法顯示連線
- LeetCode題解(面試02.08):尋找連結串列與環的交點(Python)LeetCode面試Python
- [演算法]向有序的環形單連結串列中插入新節點演算法
- 使用 TensorFlow Extended (TFX) 在生產環境中部署機器學習 丨 Google 開發者大會 2018機器學習Go
- POJ3259 Wormholes【判斷是否有負環】Worm