t3 通过90%用例不知道哪里wa了求大佬ddw
#include <unordered_set> typedef pair<int,int> pii; class Solution { public: int Length(vector<int>& niuniu, vector<int>& _niumei, vector<vector<int> >& path, int nn) { int ans=INT_MAX; vector<vector<pii>> edge(nn+1); for (vector<int> &i:path) { edge.at(i.at(0)).push_back({i.at(1),i.at(2)}); edge.at(i.at(1)).push_back({i.at(0),i.at(2)}); } unordered_set<int> niumei(_niumei.begin(),_niumei.end()); vector<bool> use(edge.size(),false); priority_queue<pii,vector<pii>,greater<pii>> helper; for (int i:niuniu) { use.at(i)=true; helper.push({0,i}); } while (!helper.empty()) { pii a=helper.top(); int value=a.first; int x=a.second; helper.pop(); if (niumei.find(x)!=niumei.end()) return value; for (pii &i:edge.at(x)) if (!use.at(i.first)) { if (niumei.find(i.first)==niumei.end()) use.at(i.first)=true; helper.push({i.second+value,i.first}); } } return -1; } };
全部评论
(2) 回帖