holy grail
题目链接
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≤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≤x,y<n,-10^9≤w≤10^9, x=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 666 lines.
Each line contains the weight of the edge you add to the graph.
INPUT
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
OUTPUT
-11
-9
-45
-15
17
7
题意:n个点m条边,给出m条边的起点终点和权值,最后给出6组数x,y,要求在x到y上加权值,问所加权值最小为多少(加边满足加完之后不能有负环,加完立即生效)。
思路:直接找y到x的最短路取相反值即可,因为这样不会有负环。因为有负边,所以用SPFA。
#include<stdio.h>
#include<algorithm>
#include<iOStream>
#include<set>
#include<map>
#include<string>
#include<string.h>
#include<math.h>
#include<vector>
#include<queue>
#define maxn 150000
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
ll dis[500];
int vis[500];
struct A
{
int to;
ll w;
};
vector <A> v[500];
void SPFA(ll s)
{
queue <int> q;
memset(dis,inf,sizeof(dis));
memset(vis,0,sizeof(vis));
dis[s]=0;
vis[s]=1;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=0;
for(int i=0;i<v[u].size();i++)
{
int x=v[u][i].to;
ll y=v[u][i].w;
if(dis[u]+y<dis[x])
{
dis[x]=dis[u]+y;
if(vis[x]==0)
{
q.push(x);
vis[x]=1;
}
}
}
}
return ;
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
int n,m,a,b;
ll c;
struct A t;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
scanf("%d %d %lld",&a,&b,&c);
t.to = b;
t.w =c;
v[a].push_back(t);
}
for(int i=0;i<6;i++){
scanf("%d %d",&a,&b);
SPFA(b);
ll ans = dis[a];
t.to = b;
t.w = (-1)*ans;
v[a].push_back(t);
printf("%lld\n",(-1)*ans);
}
for(int i=0;i<=n;i++){
v[i].clear();
}
}
return 0;
}
文章最后发布于: 2019-09-01 16:58:20
相关阅读
SUGAR LADY占尽风头,成CES ASIA 首日最大黑马
全球瞩目的2018年CES ASIA展会今天在上海正式拉开帷幕,参展对象阵容十分庞大,来自各地的科技企业各自向世界展示了他们最新的高科技
文字图片是转载:http://kernel.meizu.com/linux-thermal-framework-intro.html 代码分析是自己的分析 Linux Thermal 是Linux 系统
费马小定理是欧拉定理的一个特殊情况,欧拉定理如下: 其中φ(n)是n的欧拉函数值,表示小于n的正整数中满足gcd(n, x) = 1的数x的个数,
游戏:杀戮尖塔(Slay the spire)mod--拉格朗·月
This is a mod for SlayTheSpire!这是一个杀戮尖塔的MOD!角色名称:拉格朗·月Now, I am developing.目前正在开发中。GitHub源码: ht
@dynamic 与 @synthesize 关键词个人理解
@dynamic与@synthesize的最基本的理解 1、@property有两个对应的词,一个是@synthesize,一个是@dynamic。如果@synthesize和@dynami