日韩性视频-久久久蜜桃-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ò),歡迎將生活随笔推薦給好友。