(难) attack_1804

来源:互联网 发布:笔记本重置网络设置 编辑:程序博客网 时间:2024/06/05 11:26

其实这个题还是很多人做出来的。。只是我太渣渣了,



各种细节没有安排好。。逻辑思路没有安排好。


慢慢找BUG找出来的。



#include<iostream>#include<stdlib.h>#include<cstdio>#include<cstring>#include<vector>#include<queue>using namespace std;#define N 100010#define inf 1e15struct my{int n;long long  l;int w;my(int a,int b,long long c){n=a;w=b;l=c;}};bool operator<(my a,my b){return a.l>b.l;}struct car{int n;int w;car (int a,int b){n=a;w=b;}};vector<int>  hi[N];vector<my> go[N];int n,m;int  vist[N];int qq;int s,e,q1,q2,k;long long length;long long pre_bfs(){int i,j;priority_queue<my> q;while (!q.empty())q.pop();q.push(my(q1,0,0));vist[q1]=1;while (!q.empty()){my d=q.top();j=d.n;q.pop();//cout<<j<<' '<<d.l<<endl;if (j==q2)return d.l;int l=go[j].size();vist[j]=1;for (i=0;i<l;i++) if (!vist[go[j][i].n]){int temp=go[j][i].n;q.push(my(temp,0,d.l+go[j][i].w));}}return 0;}long long bfs(){int i,j;int l;my cur=my(s,0,0);priority_queue<my> q;while (!q.empty())q.pop();q.push(cur);vist[s]=1;long long ans=inf;int num=0;while (!q.empty()){my d=q.top();j=d.n;//cout<<j<<' '<<d.l<<endl;q.pop();l=go[j].size();vist[j]=1;if (j==q1){memset(vist,0,sizeof(vist));q.push(my(q2,1,d.l+length*(k-1)));q.push(my(q2,1,d.l));if (length)q.push(my(q1,1,d.l+length*k)),vist[q1]=1;vist[q2]=1;q1=-1;continue;}if (j==e){if (qq!=e || d.w)ans=min(ans,d.l);else continue;}for (i=0;i<l;i++) if (!vist[go[j][i].n]){int temp=go[j][i].n;if (true){//cout<<"fjkdkf"<<q1<<' '<<temp<<endl;q.push(my(temp,d.w,go[j][i].w+d.l));}else {memset(vist,0,sizeof(vist));//q.push(my(q2,0,d.l+go[j][i].w+length*(k-1)));q.push(my(q2,0,d.l+go[j][i].w));if (length)q.push(my(q1,0,d.l+go[j][i].w+length*k));vist[q1]=1;vist[q2]=1;q1=-1;/*if (vist[q2]>d.l+go[j][i].w){vist[q2]=d.l+go[j][i].w;q.push(my(q2,0,vist[q2]));}if (vist[q2]>d.l+length*(k-1)+go[j][i].w){vist[q2]=d.l+length*(k-1)+go[j][i].w;q.push(my(q2,0,vist[q2]));}if (vist[q1]>d.l+length*k+go[j][i].w){vist[q1]=d.l+length*k+go[j][i].w;q.push(my(q1,0,vist[q1]));}*/}}}return ans;}int main(){freopen("in.txt","r",stdin);int i,j;int v;int t;cin>>t;int num=0;while (t--){num++;scanf("%d%d",&n,&m);for (i=0;i<n;i++){go[i].clear();hi[i].clear();vist[i]=0;}while (m--){scanf("%d%d%d",&i,&j,&v);for (k=0;k<go[i].size();k++){if (go[i][k].n==j){if (go[i][k].w>v){go[i].erase(go[i].begin()+k);}break;}}for (k=0;k<go[j].size();k++){if (go[j][k].n==i){if (go[j][k].w>v){go[j].erase(go[j].begin()+k);}break;}}go[i].insert(go[i].end(),my(j,v,0));go[j].insert(go[j].end(),my(i,v,0));}scanf("%d%d%d%d%d",&s,&e,&q1,&q2,&k);length=pre_bfs();printf("Case #%d: ",num);//cout<<length<<endl;for (i=0;i<n;i++)vist[i]=0;qq=q1;long long ans=bfs();if (ans!=inf)printf("%lld\n",ans);elseputs("-1");}}



An A-Level Attack
Time Limit: 2000 ms Memory Limit: 65535 kB Solved: 48 Tried: 427
Submit
Status
Best Solution
Back
Description

Magic world does exist. Magic world has wars. Magic world has heroes in wars. Magic world has a hero named zplinti1.

Today zplinti1 gets a new mission. He needs to reach the enemy’s base, which is located somewhere in the Nekoneko City. He has to be as quickly as possible for a military attack.


Now zplinti1 has arrived outside Nekoneko, but he still has to find a way to the enemy base. On the map of the Nekoneko, there are n places, and some of them are connected by undirected roads. Zplinti1 decides to choose a starting position, and move along those roads to reach the enemy base.

Unfortunately, just after zplinti1 calculates all the roads’ lengths, a bad news comes. The enemy has set a special magic at one of the places in the city, which will send anyone stepping on instantly to another place. But no magic is perfect, this magic can only be effective for K times. Zplinti1 wonders the shortest time to finish his mission, or declares it as impossible.

The enemy might set magic at their own place, just at the door of the base. Zplinti1 considers his mission completed only when he is able to pass through that door.
Input

The first line of input contains a number T(T<=30), indicating the number of test cases.

For each case, there are two numbers n and m which is the number of places and roads in Nekoneko(2<= n <=100,000,0<= m <=200,000). Then m lines follow, each with three integers u, v, and w. indicating there is a road between place u and place v, which takes zplinti1 w minutes to go(0<= u,v< n,0< w <= 100,000).

After that, five numbers are given, they are s,e,p1,p2 and K(0<= s,e,p1,p2 < n,s!=e,p1!=p2,0< K <=100,000). Zplinti1 will first step into place s in the city, and the enemy’s base is at place e. The enemy set magic at place p1, which will send someone instantly to place p2. The magic will be effective to send a person K times.
Output

For each case, output “Case #i: “ first. (i is the number of the test case, from 1 to T). Then output a number which is the shortest time for zplinti1 to reach his goal. If it is impossible, output “-1”.
Sample Input

4
4 4
0 1 10
1 2 10
2 3 10
0 3 20
0 3 2 1 10
4 3
0 1 10
1 2 10
2 3 10
0 3 2 1 10
4 2
0 1 10
2 3 20
0 3 1 2 10
4 0
0 3 1 2 10
Sample Output

Case #1: 20
Case #2: 130
Case #3: 30
Case #4: -1
Source



原创粉丝点击