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
";
    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