日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

CodeForces - 1486E Paired Payment(分层图最短路)

發布時間:2024/4/11 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CodeForces - 1486E Paired Payment(分层图最短路) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接:點擊查看

題目大意:給出一個 nnn 個點 mmm 條邊組成的帶權無向圖,規定每次只能走兩條邊,假設走的兩條邊為 a?>b?>ca->b->ca?>b?>c,那么花費的權值為 (wab+wbc)2(w_{ab}+w_{bc})^2(wab?+wbc?)2,問從點 111nnn 個點的最短路分別是多少,如果不可達輸出 ?1-1?1

題目分析:讀題的時候注意到了 www 的上限只有 505050,本來以為只是限制答案不爆 intintint 的,結果說是用來建分層圖的。。

首先本題中最后的答案一定是經過偶數條邊到達的,所以自然需要先給距離數組 ddd 加一維用來維護奇偶,又因為 www 很小,可以直接用來記錄 preprepre,也就是上一條邊的權值

如此一來,當 “偶數” 轉移到 “奇數” 時,我們可以將邊權設置為 000 ,也就是隨意轉移,但是當從 “奇數” 轉移到 “偶數” 的時候,就需要利用題目中的公式配合 preprepre 計算貢獻了

代碼:

// Problem: E. Paired Payment // Contest: Codeforces - Codeforces Round #703 (Div. 2) // URL: https://codeforces.com/contest/1486/problem/E // Memory Limit: 512 MB // Time Limit: 4000 ms // // Powered by CP Editor (https://cpeditor.org)// #pragma GCC optimize(2) // #pragma GCC optimize("Ofast","inline","-ffast-math") // #pragma GCC target("avx,sse2,sse3,sse4,mmx") #include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> #include<cassert> #include<bitset> #include<list> #include<unordered_map> #define lowbit(x) x&-x using namespace std; typedef long long LL; typedef unsigned long long ull; template<typename T> inline void read(T &x) {T f=1;x=0;char ch=getchar();while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();x*=f; } template<typename T> inline void write(T x) {if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0'); } const int inf=0x3f3f3f3f; const int N=1e5+100; vector<pair<int,int>>node[N]; int d[N][55][2]; bool vis[N][55][2]; struct Node {int to,val,pre,odds;bool operator<(const Node& t)const {return val>t.val;} }; void Dijkstra(int st) {memset(d,inf,sizeof(d));priority_queue<Node>q;d[st][0][0]=0;q.push({st,0,0,0});while(q.size()) {auto it=q.top();q.pop();int u=it.to,pre=it.pre,odds=it.odds;if(vis[u][pre][odds]) {continue;}vis[u][pre][odds]=true;for(auto it:node[u]) {int v=it.first,w=it.second,nw;if(odds&1) {nw=(w+pre)*(w+pre);} else {nw=0;}if(d[u][pre][odds]+nw<d[v][w][odds^1]) {d[v][w][odds^1]=d[u][pre][odds]+nw;q.push({v,d[v][w][odds^1],w,odds^1});}}} } int main() { #ifndef ONLINE_JUDGE // freopen("data.in.txt","r",stdin); // freopen("data.out.txt","w",stdout); #endif // ios::sync_with_stdio(false);int n,m;read(n),read(m);while(m--) {int u,v,w;read(u),read(v),read(w);node[u].push_back({v,w});node[v].push_back({u,w});}Dijkstra(1);for(int i=1;i<=n;i++) {int ans=inf;for(int j=0;j<=50;j++) {ans=min(ans,d[i][j][0]);}if(ans==inf) {ans=-1;}printf("%d ",ans);}return 0; }

總結

以上是生活随笔為你收集整理的CodeForces - 1486E Paired Payment(分层图最短路)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。