3,10,[3,7,8],[(1,2)]
2
先去1号城市再去2号城市,花费为 3+7=10
城市编号1-NA[0]代表1号城市的开销A[1]代表2号城市的开销,以此类推
,
![]()
![]()
![]()
,代表去list[i].y城市之前要先去list[i].x城市
/**
* struct Point {
* int x;
* int y;
* };
*/
class Solution {
public:
/**
*
* @param N int整型 N
* @param V int整型 V
* @param A int整型一维数组 A
* @param ALen int A数组长度
* @param list Point类vector List
* @return int整型
*/
struct P{
int w, id;
bool friend operator < (P p1, P p2){
if(p1.w == p2.w)
return p1.id > p2.id;
return p1.w > p2.w;
}
};
int Travel(int N, int V, int* A, int ALen, vector<Point>& list) {
int inDegree[N+1], cnt=0;
memset(inDegree, 0, sizeof(inDegree));
vector<int> G[N+1];
priority_queue<P> q;
for(auto &p: list){
G[p.x].push_back(p.y);
inDegree[p.y]++;
}
for(int i=1;i<=N;i++)
if(inDegree[i]==0)
q.push(P{A[i-1], i});
while(!q.empty()){
P p = q.top();
q.pop();
if(p.w > V)
break;
V -= p.w;
cnt++;
for(auto &x: G[p.id]){
inDegree[x]--;
if(inDegree[x]==0)
q.push(P{A[x-1], x});
}
}
return cnt;
}
};