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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

【ZOJ - 3946】Highway Project(最短路子图,维护双权值,贪心,最小树形图)

發布時間:2023/12/10 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【ZOJ - 3946】Highway Project(最短路子图,维护双权值,贪心,最小树形图) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

Edward, the emperor of the Marjar Empire, wants to build some bidirectional highways so that he can reach other cities from the capital as fast as possible. Thus, he proposed the highway project.

The Marjar Empire has?N?cities (including the capital), indexed from 0 to?N?- 1 (the capital is 0) and there are?M?highways can be built. Building the?i-th highway costs?Ci?dollars. It takes?Di?minutes to travel between city?Xi?and?Yi?on the?i-th highway.

Edward wants to find a construction plan with minimal total time needed to reach other cities from the capital, i.e. the sum of minimal time needed to travel from the capital to city?i?(1 ≤?i?≤?N). Among all feasible plans, Edward wants to select the plan with minimal cost. Please help him to finish this task.

Input

There are multiple test cases. The first line of input contains an integer?Tindicating the number of test cases. For each test case:

The first contains two integers?N,?M?(1 ≤?N,?M?≤ 105).

Then followed by?M?lines, each line contains four integers?Xi,?Yi,?Di,?Ci?(0 ≤?Xi,?Yi?<?N, 0 <?Di,?Ci?< 105).

Output

For each test case, output two integers indicating the minimal total time and the minimal cost for the highway project when the total time is minimized.

Sample Input

2 4 5 0 3 1 1 0 1 1 1 0 2 10 10 2 1 1 1 2 3 1 2 4 5 0 3 1 1 0 1 1 1 0 2 10 10 2 1 2 1 2 3 1 2

Sample Output

4 3 4 4

題目大意:

給你一個無向圖,每條邊有一個距離c和花費d,叫你選一些邊,使得點0到其他所有點的距離之和最小,其次,使總花費最小。

解題報告:

? ?因為總題最優解一定滿足局部局部解,所以距離和最小就可以用求最短路來求解,最后統計個答案就可以。對于第二個權值的維護,可以在Dijkstra的同時貪心第二個權值,也可以先構造一個最短路子圖,求其中的最短路樹,也就是求最小樹形圖。(注意這里不是Kruskal可以解決的,,,因為其實這個新圖加的是有向邊、、、)

比如這個樣例:

不難看出,最短路子圖應該是這樣的:

而沒有由上到下的那條邊。

?

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair using namespace std; const int MAX = 2e5 + 5; struct Edge {int fr,to;ll w,c;int ne; } e[MAX]; struct Graph {struct Point {int pos;ll c;Point() {}Point(int pos,ll c):pos(pos),c(c) {}bool operator<(const Point b) const {return c > b.c;}};Edge e[MAX];int tot = 0;//總共tot條邊,編號0~(tot-1)int head[MAX];bool vis[MAX];ll d[MAX],cost[MAX];void add(int u,int v,ll w,ll c) {e[tot].fr = u;e[tot].to = v;e[tot].ne = head[u];e[tot].w = w;e[tot].c = c;head[u] = tot++;}void Dijkstra(int st) {priority_queue<Point> pq;memset(d,0x3f,sizeof d);memset(cost,0x3f,sizeof cost);memset(vis,0,sizeof vis);d[st] = 0;cost[st] = 0;pq.push(Point(st,0));while(pq.size()) {Point cur = pq.top();pq.pop();if(vis[cur.pos]) continue;vis[cur.pos] = 1;for(int i = head[cur.pos]; ~i; i = e[i].ne) {if(d[e[i].to] > d[cur.pos] + e[i].w) {d[e[i].to] = d[cur.pos] + e[i].w;cost[e[i].to] = e[i].c;pq.push(Point(e[i].to,d[e[i].to]));}else if(d[e[i].to] == d[cur.pos]+e[i].w) {if(cost[e[i].to] > e[i].c) {cost[e[i].to] = e[i].c;pq.push(Point(e[i].to,d[e[i].to]));}}}}}void id_add(int u,int v,ll w,ll c) {e[tot].fr = u;e[tot].to = v;e[tot].ne = head[u];e[tot].w = w;e[tot].c = c;head[u] = tot++;}void init(int n) {tot = 0;for(int i = 0; i<=n; i++) head[i] = -1;} } G1; int main() {int n,m;int u,v;ll w,c;int t;cin>>t;while(t--) {scanf("%d%d",&n,&m);G1.init(n);for(int i = 1; i<=m; i++) {scanf("%d%d%lld%lld",&u,&v,&w,&c);u++,v++;G1.add(u,v,w,c);G1.add(v,u,w,c); }G1.Dijkstra(1);ll ans1 = 0,ans2 = 0;for(int i = 1; i<=n; i++) ans1 += G1.d[i],ans2 += G1.cost[i];printf("%lld %lld\n",ans1,ans2);}return 0 ; }

?

總結

以上是生活随笔為你收集整理的【ZOJ - 3946】Highway Project(最短路子图,维护双权值,贪心,最小树形图)的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 波多野吉衣一区 | 久久久久久亚洲中文字幕无码 | 久久久精品国产sm调教 | 成人国产精品免费观看 | 瑟瑟网站免费 | 精品五月天 | 最新av中文字幕 | 婷婷伊人久久 | 国产精品三 | 男女瑟瑟视频 | 日韩孕交| 在线国产91 | 一曲二曲三曲在线观看中文字幕动漫 | 成人精品在线 | 性色网站 | 亚洲三区在线观看无套内射 | 国产日本欧美一区二区 | 97人人看| 97在线精品视频 | 69精品久久久 | 在线观看一区二区三区视频 | 久久精视频 | 亚洲视频免费在线观看 | 91免费黄视频 | 手机成人av在线 | 熟妇人妻久久中文字幕 | 岛国av一区二区三区 | 美女露胸软件 | 麻豆黄色网址 | 永久免费的网站入口 | 91久久在线观看 | 欧美夜夜夜 | 中文字幕在线视频网站 | 干爹你真棒插曲免费 | 成人毛片在线精品国产 | 色综合免费 | 女人十八毛片嫩草av | www av| 日本涩涩网 | 免费无码毛片一区二区app | 欧美中文日韩 | 黄金网站在线观看 | 成人片黄网站色大片免费毛片 | 伊人av网站 | 国产一伦一伦一伦 | 亚洲麻豆一区 | 欧美成在线观看 | 久久无吗视频 | 五月激情啪啪 | 男女啊啊啊 | 好吊色av| 成人av免费在线播放 | 日韩福利在线视频 | 天堂精品视频 | 91av亚洲 | 国产福利一区二区视频 | 久久久精品毛片 | 午夜激情毛片 | 7799精品视频天天看 | 最近中文字幕第一页 | 日韩在线资源 | 亚洲精品成人片在线观看精品字幕 | 在线免费精品 | 超碰123| 99色在线视频 | 久草aⅴ | 国产成人福利 | 久久久久一区二区精码av少妇 | 中日韩在线| 亚洲av无码专区在线电影 | 一区二区三区手机在线观看 | 色网站免费 | 国产欧美日韩在线观看 | 免费欧美一级片 | 午夜整容室 | 日韩高清不卡 | 又大又粗又爽18禁免费看 | 日韩成人不卡 | 成人性视频sm. | 国精产品一区一区三区有限公司杨 | 亚洲在线免费观看 | 一级大片视频 | 88av.com| 欧美亚洲黄色 | 免费超爽大片黄 | 国产精彩视频在线观看 | 91精品国产乱码久久久久久久久 | 性色一区 | 亚洲aa在线观看 | 奇米99| 免费久久av | 亚洲一区二区三区四区不卡 | 无码人妻av一区二区三区波多野 | 粉嫩av网 | 66亚洲一卡2卡新区成片发布 | 天堂中文资源在线观看 | 欧美第一页在线观看 | 午夜亚洲成人 | 永久免费未满蜜桃 |