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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【2019icpc南京站网络赛 - H】Holy Grail(最短路,spfa判负环)

發(fā)布時(shí)間:2023/12/10 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【2019icpc南京站网络赛 - H】Holy Grail(最短路,spfa判负环) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題干:

As the current heir of a wizarding family with a long history,unfortunately, you find yourself forced to participate in the cruel Holy Grail War which has a reincarnation of sixty years.However,fortunately,you summoned a Caster Servant with a powerful Noble Phantasm.When your servant launch her Noble Phantasm,it will construct a magic field,which is actually a?directed?graph?consisting of?n vertices?and?m edges.More specifically,the graph satisfies the following restrictions :

  • Does not have multiple edges(for each pair of vertices?x?and?y, there is at most one edge between this pair of vertices in the graph) and does not have self-loops(edges connecting the vertex with itself).
  • May have?negative-weighted edges.
  • Does not have a?negative-weighted loop.
  • n<=300?,?m<=500.

Currently,as your servant's Master,as long as you add extra?6?edges to the graph,you will beat the other 6 masters to win the Holy Grail.

However,you are subject to the following restrictions when you add the edges to the graph:

  • Each time you add an edge whose cost is c,it will cost you c units of Magic Value.Therefore,you need to add an edge which has the lowest weight(it's probably that you need to add an edge which has a negative weight).
  • Each time you add an edge to the graph,the graph must not have negative loops,otherwise you will be engulfed by the Holy Grail you summon.

Input

Input data contains multiple test cases. The first line of input contains integer?t?— the number of testcases (1 \le t \le 51≤t≤5).

For each test case,the first line contains two integers n,m,the number of vertices in the graph, the initial number of edges in the graph.

Then m lines follow, each line contains three integers?x, y and w?(0 \le x,y<n0≤x,y<n,-10^9?109≤w≤10^9109,?x \not = yx?=y) denoting an edge from vertices x to y (0-indexed) of weight w.

Then 6 lines follow, each line contains two integers?s,t?denoting?the starting vertex?and?the ending vertex?of the edge you need to add to the graph.

It is guaranteed that there is not an edge starting from?s to t?before you add any edges and there must exists such an edge which has the lowest weight and satisfies the above restrictions, meaning the solution absolutely exists for each query.

Output

For each test case,output?66?lines.

Each line contains the weight of the edge you add to the graph.

樣例輸入復(fù)制

1 10 15 4 7 10 7 6 3 5 3 3 1 4 11 0 6 20 9 8 25 3 0 9 1 2 15 9 0 27 5 2 0 7 3 -5 1 7 21 5 0 1 9 3 16 1 8 4 4 1 0 3 6 9 2 1 8 7 0 4

樣例輸出復(fù)制

-11 -9 -45 -15 17 7

題目大意:

給一張n個(gè)點(diǎn)m條邊的帶權(quán)有向圖(權(quán)值可能為負(fù)),問你依次進(jìn)行6個(gè)操作,每個(gè)操作給定起點(diǎn)和終點(diǎn),讓你在起點(diǎn)終點(diǎn)之間加一條邊,問你這條邊的邊權(quán)最小是多少,可以保證新圖中無負(fù)環(huán)。

解題報(bào)告:

二分權(quán)值判負(fù)環(huán)check就行了。然而標(biāo)解好像不用二分,直接找到s到t的最短路就可以了。

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<stack> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define FF first #define SS second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 500 + 5; const ll PYL = 1e12; int n,m; int tot,head[MAX]; struct Edge {int u,v;int ne;ll w; } e[MAX*100]; void add(int u,int v,ll w) {e[++tot].u = u;e[tot].v = v;e[tot].w = w;e[tot].ne = head[u];head[u] = tot; } ll dis[MAX]; int cnt[MAX]; bool vis[MAX]; bool spfa(int st) {for(int i = 1; i<=n+1; i++) dis[i] = 1e12,vis[i] = 0,cnt[i] = 0;;queue<int> q;dis[st]=0;q.push(st);vis[st]=1; cnt[st]=1;while(q.size()) {int cur = q.front();q.pop();vis[cur]=0;for(int i = head[cur]; ~i; i = e[i].ne) {int v = e[i].v;if(dis[v] <= dis[cur] + e[i].w) continue;dis[v] = dis[cur] + e[i].w;if(!vis[v]) {cnt[v]++; vis[v]=1; q.push(v);if(cnt[v] > n) return 0;//}}}return 1;//沒有負(fù)環(huán)返回1 } int U[MAX],V[MAX],W[MAX]; int main() {int T;cin>>T;while(T--) {scanf("%d%d",&n,&m);for(int i = 1; i<=m; i++) scanf("%d%d%d",&U[i],&V[i],&W[i]),U[i]++,V[i]++;for(int Q = 1; Q<=6; Q++) {ll l = 0,r = 2e12,ans,mid;int st,ed;scanf("%d%d",&st,&ed);st++,ed++;while(l<=r) {mid = (l+r)>>1;tot=0;memset(head,-1,sizeof head);for(int i = 1; i<=m; i++) add(U[i],V[i],W[i]);for(int i = 1; i<=n; i++) add(n+1,i,0);add(st,ed,mid-PYL);if(spfa(n+1)) ans = mid,r = mid-1;else l = mid+1;}U[++m] = st,V[m] = ed,W[m] = ans-PYL;printf("%lld\n",ans-PYL);}}return 0 ; }

?

總結(jié)

以上是生活随笔為你收集整理的【2019icpc南京站网络赛 - H】Holy Grail(最短路,spfa判负环)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 亚洲欧美在线视频观看 | 亚洲色p | 国产高清精品在线观看 | 人人人妻人人澡人人爽欧美一区 | 亚洲乱乱| 性高潮免费视频 | 天堂在线亚洲 | 欧美三级不卡 | 成人在线免费播放视频 | 蜜桃av一区二区 | 一区二区不卡视频 | 电影《两个尼姑》免费播放 | 欧美日韩精品一区二区三区 | 久久大奶 | 天天天干 | 人妻一区二区三区免费 | 尤物天堂 | 欧美做受69 | 小视频在线免费观看 | 天天干天天爱天天操 | 激情吧 | 女av在线| 国产免费a视频 | 啪啪啪毛片| 国产呻吟av| 亚洲国产中文字幕在线 | 欧美美女色图 | 日本一级做a爱片 | 欧美乱人伦 | 成人久久久久久 | 日本美女日批视频 | 亚洲av熟女高潮一区二区 | 日韩色在线观看 | 久久久久a | 国产在线一区二区三区四区 | 97色网 | 欧美在线91| 91精品福利在线 | 国产精品久久久久久久成人午夜 | 国产三级在线观看 | 九九热这里有精品 | 国产看真人毛片爱做a片 | 久久久久久草 | 极品美女啪啪 | 91毛片网| 免费观看日批视频 | 亚洲五码在线 | 午夜精品久久久久久久久久久久久 | 免费av影视| 国产精品系列在线播放 | 久草福利资源 | 国产无遮挡又黄又爽又色 | 黄瓜视频在线观看 | 天天摸天天射 | 人人人妻人人澡人人爽欧美一区 | 日韩av在线中文字幕 | 欧美日韩视频在线观看一区 | 日韩在线视频观看 | 97在线观看视频免费 | av午夜精品 | 国产高清一区在线观看 | 正在播放adn156松下纱荣子 | 日韩精品在线视频观看 | 久热精品免费视频 | 免费在线黄色片 | 日本色图片 | 国内精品在线播放 | 国产欧美一区二区三区在线看蜜臂 | 午夜天堂精品久久久久 | 在线免费观看视频 | 日本免费一级片 | 久草热在线视频 | 毛片9 | 欧美日韩精品区别 | 日日操夜夜爽 | xiuxiuavnet| 中国免费毛片 | 男人的天堂视频在线观看 | 日韩手机在线视频 | 欧美熟妇毛茸茸 | 成人午夜视频免费 | 亚洲石原莉奈一区二区在线观看 | 麻豆传媒网站在线观看 | 欧美女优一区 | 国产精品无码999 | 林雅儿欧洲留学恋爱日记在线 | 国产精品无码一区二区三区免费 | 日韩久久av| 国产精品视频网站 | av网在线播放 | 黄色一级大片在线免费看产 | 性感美女毛片 | 美女91网站 | 国产成年人 | 国产精品69久久久久 | 中文字幕无线码 | 国产人妖在线播放 | www.色在线| 欧美不卡高清 |