codeforces 常用模板总结

#include<bits/stdc++.h>
using namespace std;
#define fio ios_base::sync_with_stdio(false); cin.tie(NULL);
typedef long long int lli;


int main()
{
    fio;
   


    cerr << "Time : " << (double)clock() / (double)CLOCKS_PER_SEC << "s\n";
    return 0;
}

ios_base::sync_with_stdio() 的具体用法

  决定C++标准streams(cin,cout,cerr...)是否与相应的C标准程序库文件(stdin,stdout,stderr)同步,也就是是否使用相同的stream缓冲区,缺省情况是同步的,但由于同步会带来某些不必要的负担,因此该函数作用就是我们自己可以取消同步 std::ios::sync_with_stdio(false);   
注意:必须在任何io操作之前取消同步   
函数返回前一次被调用的参数值,如果未被调用过,返回true,反映标准stream的默认的值

 

Dijkstra 算法(c++ STL)

  这个貌似是求单项边的单源最短路径

测试用例:

4 5
1 2 4
1 3 5
2 3 2
2 4 3
3 4 2

#include<bits/stdc++.h>
using namespace std;
const int N=500+5;
const int inf=1e8-1;

//输入某边到某边的距离
struct edge { int to; int dist; edge(int a,int b) { to=a; dist=b; } };
//到一个点的最短距离
struct node { int id; int cost; node(int a,int b) { id=a;cost=b; } bool operator < (const node &u) const { return cost>u.cost; } }; vector<edge> g[N]; //所有的边 int dis[N]; int n,m; int main() { int a,b,c; cin>>n>>m; for(int i=0;i<m;i++) { cin>>a>>b>>c; g[a].push_back(edge(b,c)); } for(int i=0;i<n;i++) { dis[i]=inf; } priority_queue<node> q; q.push(node(0,0)); dis[0]=0; while(!q.empty()) { node x=q.top(); int u=x.cost; q.pop(); int l=g[u].size(); for(int i=0;i<l;i++) { if(dis[g[u][i].to]>dis[u]+g[u][i].dist) { dis[g[u][i].to]=dis[u]+g[u][i].dist; q.push(node(g[u][i].to,dis[g[u][i].to])); } } }
for(int i=0;i<n;i++) { cout<<dis[i]<<" "; } return 0; }

 

转载于:https://www.cnblogs.com/CMlhc/p/9502732.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值