codeforces 20C 只能用dijstra堆优化或者spfa
2017-08-15 09:57
302 查看
inf这个数没开大!!!wa了17次!
一定要注意范围……………………555555555555555555555555555555
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
using namespace std;
const int maxe=1e6+10;
const int maxv=1e5+10;
const long long inf=9e13;
int which=1;
int N,M;
int head[maxv];
struct EDGE
{
int d,w,next;
EDGE():next(-1){}
}e[maxe];
void add(int s,int d,int w)
{
e[which].d=d;
e[which].w=w;
e[which].next=head[s];
head[s]=which++;
}
void init()
{
memset(e,0,sizeof(e));
memset(head,-1,sizeof(head));
}
struct p
{
int pn,dist;
friend bool operator<(p a,p b)
{
return a.dist>b.dist;
}
};
long long dis[maxv];
int pre[maxv];
priority_queue<p> Q;
bool settled[maxv];
void dij(int st)
{
while(!Q.empty()) Q.pop();
memset(settled,0,sizeof(settled));
for(int i=1;i<=N;i++) dis[i]=inf;
dis[st]=0;
p stt;
stt.pn=st,stt.dist=0;
Q.push(stt);
while(!Q.empty())
{
p t=Q.top();
Q.pop();
//if(t.pn==N) return 1;
if(settled[t.pn]) continue;
settled[t.pn]=1;
for(int i=head[t.pn];i!=-1;i=e[i].next)
{
int to=e[i].d;
int mid=t.pn;
if(!settled[to]&&dis[to]>dis[mid]+e[i].w)
{
dis[to]=dis[mid]+e[i].w;
p tt;
tt.pn=to;
tt.dist=dis[to];
pre[tt.pn]=t.pn;
Q.push(tt);
}
}
}
//return 0;
}
void Find(int x)
{
if(x!=1) Find(pre[x]);
if(x!=1) printf(" ");
printf("%d",x);
}
int main()
{
//cout<<inf<<endl;
while(scanf("%d%d",&N,&M)==2)
{
which=1;
init();
memset(pre,0,sizeof(pre));
int t1,t2,t3;
for(int i=1;i<=M;i++)
{
scanf("%d%d%d",&t1,&t2,&t3);
add(t1,t2,t3);
add(t2,t1,t3);
}
dij(1);
if(dis
!=inf) Find(N);
else printf("-1");
printf("\n");
}
}
一定要注意范围……………………555555555555555555555555555555
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
using namespace std;
const int maxe=1e6+10;
const int maxv=1e5+10;
const long long inf=9e13;
int which=1;
int N,M;
int head[maxv];
struct EDGE
{
int d,w,next;
EDGE():next(-1){}
}e[maxe];
void add(int s,int d,int w)
{
e[which].d=d;
e[which].w=w;
e[which].next=head[s];
head[s]=which++;
}
void init()
{
memset(e,0,sizeof(e));
memset(head,-1,sizeof(head));
}
struct p
{
int pn,dist;
friend bool operator<(p a,p b)
{
return a.dist>b.dist;
}
};
long long dis[maxv];
int pre[maxv];
priority_queue<p> Q;
bool settled[maxv];
void dij(int st)
{
while(!Q.empty()) Q.pop();
memset(settled,0,sizeof(settled));
for(int i=1;i<=N;i++) dis[i]=inf;
dis[st]=0;
p stt;
stt.pn=st,stt.dist=0;
Q.push(stt);
while(!Q.empty())
{
p t=Q.top();
Q.pop();
//if(t.pn==N) return 1;
if(settled[t.pn]) continue;
settled[t.pn]=1;
for(int i=head[t.pn];i!=-1;i=e[i].next)
{
int to=e[i].d;
int mid=t.pn;
if(!settled[to]&&dis[to]>dis[mid]+e[i].w)
{
dis[to]=dis[mid]+e[i].w;
p tt;
tt.pn=to;
tt.dist=dis[to];
pre[tt.pn]=t.pn;
Q.push(tt);
}
}
}
//return 0;
}
void Find(int x)
{
if(x!=1) Find(pre[x]);
if(x!=1) printf(" ");
printf("%d",x);
}
int main()
{
//cout<<inf<<endl;
while(scanf("%d%d",&N,&M)==2)
{
which=1;
init();
memset(pre,0,sizeof(pre));
int t1,t2,t3;
for(int i=1;i<=M;i++)
{
scanf("%d%d%d",&t1,&t2,&t3);
add(t1,t2,t3);
add(t2,t1,t3);
}
dij(1);
if(dis
!=inf) Find(N);
else printf("-1");
printf("\n");
}
}
相关文章推荐
- TOJ 2217. Invitation Cards【最短路dijkstra+堆优化或者SPFA】
- TOJ 2217. Invitation Cards【最短路dijkstra+堆优化或者SPFA】
- Codeforces 938D Buy a Ticket 【spfa优化】
- 复习最短路 spfa+dijstra堆优化
- #278 – 允许Image只能变小或者只能变大(Allow an Image to Get Bigger, But Not Smaller (or Vice Versa))
- 最短路--spfa+队列优化模板
- JS通过正则限制 input 输入框只能输入整数、小数(金额或者现金) 两位小数
- Response.End,Resonse.Rediect,Server.transfer执行后会报异常(由于代码已经过优化或者本机框架位于调用堆栈之上,无法计算表达式的值)的问题解决方案
- CodeForces-714C. Sonya and Queries(状压优化)
- 【codeforces 676C】 【尺取法】【从一个ab序列中最多改变k个字符找到最长的连续a子串或者b子串长度】
- 【Codeforces 597C】【DP 树状数组优化】Subsequences 【n个不同数,长度为k+1的LIS数】
- 【CodeForces】471D MUH and Cube Walls KMP或者字符串HASH
- spfa 的 slf 优化
- Codeforces 55D Beautiful numbers 数位dp 数论 优化技巧
- 如何限制对象只能建立在堆上或者栈上
- N阶台阶,每次只能走一个台阶或者两个台阶,求一共有多少种走法?
- BUG:由于代码已经过优化或者本机框架位于调用堆栈之上,无法计算表达式的值
- Asp.Net异常:"由于代码已经过优化或者本机框架位于调用堆栈之上,无法计算表达式的值"的解决方法
- 【codeforces】gym 101138 K. The World of Trains【前缀和优化dp】
- 关于T-SQL中exists或者not exists子查询的“伪优化”的做法