体会:用这个学到了vector,,,priority_queue,及其一些错误的使用。
先是一个简短,正确的代码,,后面是自己开始写得很失传的代码,
#include<iostream>
#include<stdlib.h>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<string.h>
#include<stack>
#include<math.h>
#include<stdlib.h>
#include<list>
#include<vector>
using namespace std;
#define N 100001
struct my
{
int num;
int w;
};
vector <my> map[N];
bool vist[N];
int n,m;
int s,e;
bool operator<(const my &a,const my &b) //这里是用于priority_queue<my>中的判断,不然会出错,,
{
return a.w<b.w;
}
int dij()
{
int i,j,k;
priority_queue<my> q;
while (!q.empty())
q.pop();
my p;
p.num=s;
p.w=2000000002;
q.push(p);
while (!q.empty())
{
my temp=q.top();
q.pop();
if (temp.num==e)
return temp.w;
vist[temp.num]=true;
for (i=0;i<map[temp.num].size();i++)
{
j=map[temp.num][i].num;
k=map[temp.num][i].w;
if (!vist[j])
{
my cur;
cur.num=j;
cur.w=min(temp.w,k);
q.push(my(cur));
}
}
}
}
int main()
{
freopen("fuck.txt","r",stdin);
int i,j,k;
my cur;
while (scanf("%d%d",&n,&m)!=EOF)
{
for (i=1;i<=n;i++)
{
map[i].clear();
vist[i]=false;
}
while (m--)
{
scanf("%d%d%d",&i,&j,&k);
cur.num=j;
cur.w=k;
map[i].push_back(cur);
cur.num=i;
map[j].push_back(cur);
}
scanf("%d%d",&s,&e);
printf("%d\n",dij());
//cout<<"fdjkjkf";
}
}
未成功能代码,一直RE,链表,不熟,还花了这么多的时间,
#include<iostream>
#include<stdlib.h>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<string.h>
#include<stack>
#include<math.h>
#include<stdlib.h>
#include<list>
#include<vector>
using namespace std;
#define N 100001
#define Inf 2000000004
struct my
{
int num;
int w;
my *next;
}*p[N],*c[N];
int n;
int m;
int s,e;
int dis[N];
bool vist[N];
int ans;
bool f;
my mm[N];
int M;
void put()
{
int i,j,k;
for (i=1;i<=n;i++)
cout<<dis[i]<<' ';
cout<<endl;
}
void addp(int i,int j,int k)
{
my *cur=new my;
//cout<<i<<' '<<j<<' '<<k<<endl;
cur->num=j;
cur->w=k;
cur->next=NULL;
c[i]->next=cur;
c[i]=c[i]->next;
}
void dij()
{
int i,j,k;
my *cur;
for (k=1;k<=n;k++)
{
int big=-1;
for (i=1;i<=n;i++)
if (!vist[i]&&big<dis[i])
big=dis[i],j=i;
vist[j]=true;
ans=ans>big?big:ans;
//cout<<ans<<' '<<j<<' '<<big<<endl;
if (j==e)
{
printf("%d\n",ans);
return;
}
cur=p[j];
while (cur->next!=NULL)
{
cur=cur->next;
if (!vist[cur->num]&&dis[cur->num]<cur->w)
dis[cur->num]=cur->w;
}
}
}
bool cmp(my a,my b)
{
return a.w>b.w;
}
void qq()
{
int i,j,k;
queue<int> q;
while (!q.empty())
q.pop();
q.push(s);
my *cur;
vist[s]=true;
while (!q.empty())
{
int d=q.front();
//cout<<d<<endl;
q.pop();
cur=p[d];
M=0;
while (cur->next!=NULL)
{
//cout<<M<<endl;
cur=cur->next;
if (!vist[cur->num])
{
vist[cur->num]=true;
mm[M].num=cur->num;
mm[M++].w=cur->w;
dis[cur->num]=min(cur->w,dis[d]);
//cout<<M<<endl;
}
}
sort(mm,mm+M,cmp);
for (i=0;i<M;i++)
q.push(mm[i].num);
}
}
int main()
{
freopen("fuck.txt","r",stdin);
int i,j,k;
while (scanf("%d%d",&n,&m))
{
for (i=1;i<=n;i++)
{
p[i]=new my;
p[i]->next=NULL;
c[i]=p[i];
}
while (m--)
{
scanf("%d%d%d",&i,&j,&k);//cin>>i>>j>>k;
addp(i,j,k);
addp(j,i,k);
}
scanf("%d%d",&s,&e);//cin>>s>>e;
//put();
for (i=1;i<=n;i++)
{
vist[i]=false;
}
ans=Inf;
dis[s]=Inf;
//dij();
qq();
//put();
printf("%d\n",dis[e]);
}
return 0;
}
Background
"Now, you are getting closer and closer to Your Gate to Freedom!
And you will pass the most dangerous paths! Be careful!"
We're in a maze now. There're many rooms floating in the sky connected with wooden bridges. Bridges seems unstable...
Problem
At first, I'm in room X, and I want to go to room Y. There're N (N <= 100,000) rooms connected with M (M <= 1,000,000) edges. Each edge has a limit of weight, if weight exceeds this value, the edge will split, and drop me into the river.
How many things I can take to room Y at most?
Input
This problem contains multiple test cases.
Each test case begins with two integers N and M, then M lines, each line has 3 numbers: X, Y and Weight (<=2,000,000,000).
Output
For each test case, output the maximum weight I can take.
Sample Input
6 6
1 5 2000
2 4 5000
2 5 3300
3 4 2400
3 6 2200
4 6 6000
3 5
Sample Output
2400
Contest: A Great Beloved and My Gate to Freedom