算法分析

$dis_{i,j}=min\{f_{k,i-cost_{k,i}}+time_{k,i} | edge_{k,i}\}$

代码实现

#include <bits/stdc++.h>
using namespace std;
// IO优化
namespace IO
{
char buf[1<<15],*fs,*ft;
char out[1<<28],*fe=out;
inline char getc(){
}

template <typename T>
x=0;
bool f=false;
char ch=getchar();
while (!isdigit(ch) && ch^'-') ch=getchar();
if (ch=='-') f=true, ch=getchar();
while (isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48), ch=getchar();
if(f)
x=-x;
return;
}

inline void flush(){
fwrite(out,1,fe-out,stdout);
fe=out;
}

template <typename T>
inline void writeln(T x){
if (!x) *fe++=48;
if (x<0) *fe++='-', x=-x;
T num=0, ch[20];
while (x) ch[++num]=x%10+48, x/=10;
while (num) *fe++=ch[num--];
*fe++='\n';
}

template <typename T>
inline void writesp(T x){
if (!x) *fe++=48;
if (x<0) *fe++='-', x=-x;
T num=0, ch[20];
while (x) ch[++num]=x%10+48, x/=10;
while (num) *fe++=ch[num--];
*fe++=' ';
}
}

int tot,n,m,s,t,ans;
// 树状数组类体
class BinaryIndexedTree
{
private:
int Arr[100005];
int lowbit(int x){
return x&-x;
}
public:
void modify(int pos,int val){ // 修改，维护最小值
pos++;
for(;pos<=n*100;pos+=lowbit(pos)){
Arr[pos]=min(Arr[pos],val);
}
}

int query(int pos){ // 查询最小值
++pos;
int ans=0x3f3f3f3f;
for(;pos;pos-=lowbit(pos)){
ans=min(ans,Arr[pos]);
}
return ans;
}

void clear(){ // 清空
memset(Arr,0x3f,sizeof(Arr));
}
};

struct Edge
{
int To,Next,Weight,Time; // 链式前向星
};

BinaryIndexedTree tree[105];
Edge e[605];
int dis[105][100005],vis[105][100005];

void link(int from,int to,int len,int tim){
e[++tot].To=to;
e[tot].Weight=len;
e[tot].Time=tim;
}

void spfa(int start){
queue<pair<int,int> > q;
memset(vis,0,sizeof(vis));
memset(dis,0x3f,sizeof(dis));
for(int i=0;i<=n;i++) tree[i].clear();
q.push(make_pair(start,0));
tree[start].modify(0,0);
dis[start][0]=0;
vis[start][0]=1;
while(!q.empty()){
int u=q.front().first,w=q.front().second;
q.pop();
vis[u][w]=0;
int v=e[i].To,neww=w+e[i].Weight; // w：费用，neww：新的费用
if(tree[v].query(neww)>dis[u][w]+e[i].Time){ // 剪枝
dis[v][neww]=dis[u][w]+e[i].Time; // 更新
tree[v].modify(neww,dis[v][neww]);
if(!vis[v][neww]){
vis[v][neww]=1;
q.push(make_pair(v,neww));
}
}
}
}
}

int main(){
for(int i=1;i<=m;i++){
int x,y,l,t;
}
spfa(s);
int now=dis[0][0];
for(int i=0;i<=n*100;i++){
if(dis[t][i]<now){
ans++;
now=dis[t][i];
}
}
IO::writeln(ans);
IO::flush();
return 0;
}

#include <bits/stdc++.h>
using namespace std;

namespace IO
{
char buf[1<<15],*fs,*ft;
char out[1<<28],*fe=out;
inline char getc(){
}

template <typename T>
x=0;
bool f=false;
char ch=getchar();
while (!isdigit(ch) && ch^'-') ch=getchar();
if (ch=='-') f=true, ch=getchar();
while (isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48), ch=getchar();
if(f)
x=-x;
return;
}

inline void flush(){
fwrite(out,1,fe-out,stdout);
fe=out;
}

template <typename T>
inline void writeln(T x){
if (!x) *fe++=48;
if (x<0) *fe++='-', x=-x;
T num=0, ch[20];
while (x) ch[++num]=x%10+48, x/=10;
while (num) *fe++=ch[num--];
*fe++='\n';
}

template <typename T>
inline void writesp(T x){
if (!x) *fe++=48;
if (x<0) *fe++='-', x=-x;
T num=0, ch[20];
while (x) ch[++num]=x%10+48, x/=10;
while (num) *fe++=ch[num--];
*fe++=' ';
}
}

const int lim = 1e4 + 5;
int nxt[605], tim[605], cost[605], edge[605], head[605];
int q[1000005][2], dis[105][lim], vis[105][lim];
int minval[105][lim];
int tot, n, m, s, t, ans;

void link(int x, int y, int c, int t) {
edge[tot] = y;
tim[tot] = t;
cost[tot] = c;
}

int query(int x, int y) {
++y;
int mn = 1e7;
for (; y; y -= (y & -y)) {
mn = min(mn, minval[x][y]);
}
return mn;
}

void modify(int x, int y, int val) {
++y;
for (; y <= n * 100; y += (y & -y)) {
minval[x][y] = min(minval[x][y], val);
}
}

void spfa() {
memset(dis, 63, sizeof(dis));
memset(minval, 63, sizeof(minval));
q[1][0] = s;
q[1][1] = 0;
dis[s][0] = 0;
vis[s][0] = 1;
modify(s, 0, 0);
for (int front = 1, back = 1; front <= back; ++front) {
int x = q[front][0], c = q[front][1];
vis[x][c] = 0;
for (int i = head[x], y; y = edge[i], i; i = nxt[i]) {
int newc = c + cost[i];
if (query(y, newc) > dis[x][c] + tim[i]) {
dis[y][newc] = dis[x][c] + tim[i];
modify(y, newc, dis[y][newc]);
if (!vis[y][newc]) {
vis[y][newc] = 1;
q[++back][0] = y;
q[back][1] = newc;
}
}
}
}
}

int main() {
for (int i = 1; i <= m; ++i) {
int x, y, c, t;
}
spfa();
int now = 0x3f3f3f3f;
for (int i = 0; i <= n * 100; ++i) {
if (dis[t][i] < now) {
++ans;
now = dis[t][i];
}
}
IO::writeln(ans);
IO::flush();
return 0;
}
posted @ 2019-10-25 14:10  ctjcalc  阅读(...)  评论(...编辑  收藏