struct ShortestPath
{
typedef int type;
static const int inf=1e9;
static const int __=100005;
struct edge
{
int nex;type dis;
edge() {}
edge(int x,type y):
nex(x),dis(y) {}
bool operator<(const edge& b)const
{
return dis>b.dis;
}
};
int n;
type dis[__];
bool vis[__];
vector<edge>G[__];
priority_queue<edge>Q;
type& operator[](int x){return dis[x];}
void init(int _n)
{
n=_n;
for(int i=1;i<=n;++i)
{
vis[i]=false;
dis[i]=inf;
}
}
void add_edge(int x,int y,type dis)
{
G[x].push_back(edge(y,dis));
}
//先調(diào)用init
void Dijkstra(int st)
{
dis[st]=0,Q.push(edge(st,0));
while(!Q.empty())
{
int t=Q.top().nex;Q.pop();
if(vis[t])continue;
vis[t]=true;
for(edge &x:G[t])
if(!vis[x.nex] && dis[t]+x.dis<dis[x.nex])
{
dis[x.nex]=dis[t]+x.dis;
Q.push(edge(x.nex,dis[x.nex]));
}
}
}
void clear()
{
for(int i=1;i<=n;++i)
G[i].clear();
}
}D;
Shortest Path
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- [編程題]比較重量小明陪小紅去看鉆石,他們從一堆鉆石中隨機(jī)抽取兩顆并比較她們的重量。這些鉆石的重量各不相同。在他們...