入报名链接)~ |
pathTime Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1411 Accepted Submission(s): 300 Problem Description You have a directed weighted graph with n vertexes and m edges. The value of a path is the sum of the weight of the edges you passed. Note that you can pass any edge any times and every time you pass it you will gain the weight.
Input The input consists of multiple test cases, starting with an integer t (1≤t≤100), denoting the number of the test cases.
Output For each query, print one integer indicates the answer in line.
Sample Input 1 2 2 2 1 2 1 2 1 2 3 4
Sample Output 3 3 Hint 1->2 value :1 2->1 value: 2 1-> 2-> 1 value: 3 2-> 1-> 2 value: 3 |
题意:给出一个带权有向图,每次询问图中第K小的路径权重。
解法之一:首先将邻接表的边权按升序排列,利用优先队列维护拓展出来的路径的大小,直至拓展完最大的K。(代码思路参考至群里一位大佬的代码QAQ tql)
#include
#include
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn=50010;
struct node
{
int to;
ll cost;
bool operator < (const node &a) const
{
return cost>a.cost;
}
};
bool cmp(node a,node b)
{
return a.cost q1;
priority_queue,less >q2;
vector g[maxn];
ll ans[maxn];
int query[maxn];
int main()
{
int n,m,q;
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d %d %d",&n,&m,&q);
while(!q1.empty()) q1.pop();
while(!q2.empty()) q2.pop();
memset(ans,0,sizeof ans);
for(int i=1;i<=n;i++) g[i].clear();
for(int i=1;i<=m;i++)
{
int u,v;ll w;
scanf("%d %d %lld",&u,&v,&w);
node t;
t.to=v,t.cost=w;
g[u].push_back(t);
q1.push(t);
q2.push(w);
}
for(int i=1;i<=n;i++)
{
sort(g[i].begin(),g[i].end(),cmp);
}
int mx=0;
for(int i=1;i<=q;i++)
{
scanf("%d",&query[i]);
mx=max(mx,query[i]);
}
for(int i=1;i<=mx;i++)
{
ans[i]=q1.top().cost;
int v=q1.top().to;
q1.pop();
for(auto x:g[v])
{
ll nowcost=ans[i]+x.cost;
ll y=x.to;
if(q2.size()==mx)
{
if(nowcost>q2.top())
{
break;
}
else
{
q2.pop();
q2.push(nowcost);
q1.push(node{y,nowcost});
}
}
else{
q1.push(node{y,nowcost});
q2.push(nowcost);
}
}
}
for(int i=1;i<=q;i++)
{
printf("%lld\n",ans[query[i]]);
}
}
return 0;
}
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1122 Accepted Submission(s): 415
Problem Description
You are given an array a1,a2,...,an(∀i∈[1,n],1≤ai≤n). Initially, each element of the array is **unique**.
Moreover, there are m instructions.
Each instruction is in one of the following two formats:
1. (1,pos),indicating to change the value of apos to apos+10,000,000;
2. (2,r,k),indicating to ask the minimum value which is **not equal** to any ai ( 1≤i≤r ) and **not less ** than k.
Please print all results of the instructions in format 2.
Input
The first line of the input contains an integer T(1≤T≤10), denoting the number of test cases.
In each test case, there are two integers n(1≤n≤100,000),m(1≤m≤100,000) in the first line, denoting the size of array a and the number of instructions.
In the second line, there are n distinct integers a1,a2,...,an (∀i∈[1,n],1≤ai≤n),denoting the array.
For the following m lines, each line is of format (1,t1) or (2,t2,t3).
The parameters of each instruction are generated by such way :
For instructions in format 1 , we defined pos=t1⊕LastAns . (It is promised that 1≤pos≤n)
For instructions in format 2 , we defined r=t2⊕LastAns,k=t3⊕LastAns. (It is promised that 1≤r≤n,1≤k≤n )
(Note that ⊕ means the bitwise XOR operator. )
Before the first instruction of each test case, LastAns is equal to 0 .After each instruction in format 2, LastAns will be changed to the result of that instruction.
(∑n≤510,000,∑m≤510,000 )
Output
For each instruction in format 2, output the answer in one line.
Sample Input
3
5 9
4 3 1 2 5
2 1 1
2 2 2
2 6 7
2 1 3
2 6 3
2 0 4
1 5
2 3 7
2 4 3
10 6
1 2 4 6 3 5 9 10 7 8
2 7 2
1 2
2 0 5
2 11 10
1 3
2 3 2
10 10
9 7 5 3 4 10 6 2 1 8
1 10
2 8 9
1 12
2 15 15
1 12
2 1 3
1 9
1 12
2 2 2
1 9
Sample Output
1
5
2
2
5
6
1
6
7
3
11
10
11
4
8
11
Hint
note: After the generation procedure ,the instructions of the first test case are : 2 1 1, in format 2 and r=1 , k=1 2 3 3, in format 2 and r=3 , k=3 2 3 2, in format 2 and r=3 , k=2 2 3 1, in format 2 and r=3 , k=1 2 4 1, in format 2 and r=4 , k=1 2 5 1, in format 2 and r=5 , k=1 1 3 , in format 1 and pos=3 2 5 1, in format 2 and r=5 , k=1 2 5 2, in format 2 and r=5 , k=2 the instructions of the second test case are : 2 7 2, in format 2 and r=7 , k=2 1 5 , in format 1 and pos=5 2 7 2, in format 2 and r=7 , k=2 2 8 9, in format 2 and r=8 , k=9 1 8 , in format 1 and pos=8 2 8 9, in format 2 and r=8 , k=9 the instructions of the third test case are : 1 10 , in format 1 and pos=10 2 8 9 , in format 2 and r=8 , k=9 1 7 , in format 1 and pos=7 2 4 4 , in format 2 and r=4 , k=4 1 8 , in format 1 and pos=8 2 5 7 , in format 2 and r=5 , k=7 1 1 , in format 1 and pos=1 1 4 , in format 1 and pos=4 2 10 10, in format 2 and r=10 , k=10 1 2 , in format 1 and pos=2
题意:给出一个长度为n的数组,数组内的数是各不相同。有m个操作,操作1是给a[pos]的数加上1000000,操作2是查询一个最小的没有在1~R出现过的数并且不小于K。
解法之一:建立权值线段树,每个节点维护该权值范围的最大下标。
#include
#include
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
int n,m;
const int maxn=100010;
int a[maxn];
int cnt;
struct node
{
int l,r;
int index;
}tree[maxn<<2];
void pushup(int rt)
{
tree[rt].index=max(tree[rt<<1].index,tree[rt<<1|1].index);
}
void build(int rt,int l,int r)
{
tree[rt].l=l;tree[rt].r=r;
tree[rt].index=n+1;
if(l==r) return ;
int mid=(l+r)>>1;
build(rt<<1,l,mid);
build(rt<<1|1,mid+1,r);
}
void update(int rt,int p,int v)
{
if(tree[rt].l==tree[rt].r)
{
tree[rt].index=v;
return;
}
int mid=(tree[rt].l+tree[rt].r)>>1;
if(mid>=p) update(rt<<1,p,v);
else update(rt<<1|1,p,v);
pushup(rt);
}
int query(int rt,int k,int R)
{
if(tree[rt].l==tree[rt].r)
{
if(tree[rt].index==n+1)
{
cnt=tree[rt].l;
}
return tree[rt].index;
}
int mid=(tree[rt].l+tree[rt].r)>>1;
int ans=0;
if(mid>=k)
{
if(tree[rt<<1].index>R)//左子树的最大下标如果大于所求的 则查询左子树
{
ans=query(rt<<1,k,R);
if(ans>=inf) ans=query(rt<<1|1,k,R);
if(ans<=R) ans=query(rt<<1|1,k,R);//如果左子树没有符合条件的,则查询右子树
}
else{
ans=query(rt<<1|1,k,R);
}
}
else ans=query(rt<<1|1,k,R);
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&m);
build(1,1,100000);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
update(1,a[i],i);
}
int t1,t2,lasans=0,op;
while(m--)
{
scanf("%d",&op);
if(op==1)
{
scanf("%d",&t1);
int pos=(t1^lasans);
update(1,a[pos],n+1);
}
else{
scanf("%d %d",&t1,&t2);
int r=(t1^lasans);
int K=(t2^lasans);
int pos=query(1,K,r);
if(pos==n+1)
{
printf("%d\n",cnt);
lasans=cnt;
}
else
{
printf("%d\n",a[pos]);
lasans=a[pos];
}
}
}
}
return 0;
}