阿里秋招C/C++程式設計題
阿新 • • 發佈:2019-01-26
題目:
伺服器都有上下游依賴關係,輸入第一行表示伺服器的數量,和依賴關係的個數。如下表示有5臺伺服器,4個依賴關係。
接下來的5行是每臺伺服器的啟動延遲(單位ms),最後四行是伺服器之間的依賴關係(5臺伺服器用數字0-4表示)。
不考慮存在環。
輸出要求:延遲最高的路徑的延遲時間和此路徑的伺服器數
輸入樣例:
5 4
3
5
12
6
4
0 1
0 2
2 3
3 4
輸出:
25 4
#include <stdio.h>
struct linkTime
{
int count;
int latency;
// int node[100]; //儲存每條路徑所包含的節點
};
/*儲存每條可能的路徑的節點數和總延遲,可以直接使用二維陣列,
**但我更喜歡這樣的自定義資料結構,讓我思考解題方法時思路更清晰,並且更加容易擴充套件。
*/
void LinkTime(int node, int sociate);
int main()
{
int node;
int sociate;
scanf("%d%d", &node, &sociate);
LinkTime(node, sociate);
return 0;
}
void LinkTime(int node, int sociate)
{
int time[node];
int soc[sociate][2];
struct linkTime lt[node];
/*如果在vc++6.0裡編譯,則這三句無法通過。
**因為vc++6.0裡不允許用變數名作為陣列的大小初始化陣列,即使該變數已經賦值
*/
int linkCount = 0;
int count, latency;
int flag[sociate]; //標識每條邊是否可作為路徑的進入邊,0為是,1為否
int preNode;
int maxLatency;
int i, j;
for(i=0; i<node; i++)
scanf("%d", &time[i]);
for(i=0; i<sociate; i++)
scanf("%d%d", &soc[i][0], &soc[i][1]);
for (i=0; i<sociate; i++)
{
if(flag[i] == 1)
continue;
count = 2;
latency = time[soc[i][0]] +time[soc[i][1]];
preNode = soc[i][1];
for(j=i+1; j<sociate; j++)
{
if(soc[j][0] == preNode)
{
flag[j] =1;
count ++;
latency = latency + time[soc[j][1]];
preNode = soc[j][1];
}
}
lt[linkCount].count = count;
lt[linkCount].latency = latency;
linkCount ++;
}
maxLatency = lt[0].latency;
count = lt[0].count;
for(i=1; i<linkCount; i++)
{
if(lt[i].latency > maxLatency)
{
maxLatency = lt[i].latency;
count = lt[i].count;
}
}
printf("%d %d\n", maxLatency, count);
}
/*****************************************
Date:2018/7/18
Author:ZNM
*****************************************/